2012-05-25 220 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