2012-05-25 204 views
1

我怎样才能得到我的FOREACH我的DataGridView的下一行的值的值我怎样才能让我的DataGridView的下一行

foreach (DataGridViewRow row in dataGridViewSelection.Rows) 
{ 
     if ((bool)((DataGridViewCheckBoxCell)row.Cells[3]).Value) 
     { 
      do 
      { 
       list.Add(row.Cells[1].Value.ToString()); 
      } 
      while (row.Cells[2].Value == the next row.Cells[2].Value-->of the next row); 

     }    
} 

我想获得在同一单元格的值下一排,我可以比较它们。 谢谢

回答

2

你需要使用for循环,而不是foreach,但这是简单,因为DataGridViewRowCollection实现所需的信息:

for (int rowNum=0;rowNum<dataGridViewSelection.Rows.Count - 1; ++rowNum) 
    { 
      DataGridViewRow row = dataGridViewSelection.Rows[rowNum]; 
      if ((bool)((DataGridViewCheckBoxCell)row.Cells[3]).Value)     { 
       do 
       { 
        list.Add(row.Cells[1].Value.ToString()); 
       } while (row.Cells[2].Value == dataGridViewSelection.Rows[rowNum+1].Cells[2].Value); 

      }    
    } 
通过索引

通过索引,你可以轻松地访问任何其他在你的循环中排。

+0

非常感谢,非常感谢 – pharaon450