Click here to Skip to main content
15,906,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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++;
}
Posted
Updated 18-Mar-14 22:59pm
v2
Comments
Kornfeld Eliyahu Peter 19-Mar-14 4:59am    
And the question is?
Bernhard Hiller 19-Mar-14 5:24am    
((DataTable)GridView1.DataSource).Merge(dt2);
GridView1.DataSource = (DataTable)GridView1.DataSource;
GridView1.DataBind();
What's that? And I guess that's where you encounter a problem...

1 solution

You didn't define the i you're using in the if line. Maybe that's outside of the code snippet you posted.

For me, it would be easier to read
C#
for(int rowIndex = 1; rowIndex < GridView1.Rows.Count; i++)
{
    if(GridView1.Rows[i - 1].Cells[4].Text == "Whatever")
    {
        DoSomething(GridView1.Rows[i - 1]);
    }
}
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.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900