本文介绍了更改for循环中的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
foreach (GridViewRow row in GridView1.Rows) { if (GridView1.Rows[i - 1].Cells[4].Text == "SFG") { DataTable dt2 = new DataTable(); System.Data.OleDb.OleDbCommand sqlCmd = new System.Data.OleDb.OleDbCommand("SELECT * FROM BOM1_HH WHERE MODEL_NAME='" + GridView1.Rows[i - 1].Cells[2].Text + "'", (System.Data.OleDb.OleDbConnection)Session["CON"]); System.Data.OleDb.OleDbDataAdapter sqlDa = new System.Data.OleDb.OleDbDataAdapter(sqlCmd); sqlDa.Fill(dt2); ((DataTable)GridView1.DataSource).Merge(dt2); GridView1.DataSource = (DataTable)GridView1.DataSource; GridView1.DataBind(); } i++; }
推荐答案
for(int rowIndex = 1; rowIndex < GridView1.Rows.Count; i++) { if(GridView1.Rows[i - 1].Cells[4].Text == "Whatever") { DoSomething(GridView1.Rows[i - 1]); } }
但是你为什么要在 1 而不是通常的开始你的迭代> 0 然后总是使用 i - 1 ?是否有更多的代码实际上使用 i 对两个相邻的行做某事?
否则你会更好从 0 开始,只使用 i 。
或者完全忘记迭代变量使用问题代码中的行变量。
But why would you want to start your iteration at 1 instead of the usual 0 and then always use i - 1? Is there more code that acutally uses i to do something to two adjacent rows?
Otherwise you would be better off starting at 0 and using just i.
Or forget about iteration variables altogether and use the row variable from your question's code.
这篇关于更改for循环中的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!