2012-08-23 85 views
0

我需要爲每個語句提供一些幫助,基本上發生的是當用戶在單元格中編輯一個值時,我的foreach會將此應用於數據網格中的所有單元格並將其全部更改爲i需要我的foreach語句通過數據網格迭代工作,但只改變已被編輯選中行,但它似乎是更新的所有行計分,這應該是隻有當前排foreach語句c#

foreach (DataGridViewRow row in dgvDetials.Rows) //loop through each of the results 
      { 
       string scrapDate, scrapShift, PartCode; 
       scrapDate = dtpInspectionDate.Value.ToString("MM/dd/yyyy"); 
       scrapShift = cbShift.Text; 

       PartCode = row.Cells[0].Value.ToString(); 

       //define each of the counts 
       int qtyInspCount = SQLMethods.CountQtyInspTotal(scrapDate, scrapShift, area, PartCode); 
       int scrapCount = SQLMethods.CountScrapReworkTotal(scrapDate, scrapShift, area, PartCode, "S"); 
       int reworkCount = SQLMethods.CountScrapReworkTotal(scrapDate, scrapShift, area, PartCode, "R"); 

       //populate each of the counts cells 
       row.Cells[2].Value = 0; 
       row.Cells[5].Value = qtyInspCount; 
       row.Cells[3].Value = scrapCount; 
       row.Cells[4].Value = reworkCount; 
      } 
+0

爲什麼有'foreach'?您可以直接獲取編輯的單元格。 – ChrisF

+0

那你爲什麼需要遍歷它們呢?只需獲取編輯的行索引。 –

+0

我不明白你的問題,爲什麼通過*所有*行循環,如果你只想改變其中之一? – CodingGorilla

回答

2

您只需更改當前行。
然後你應該避免在整個數據網格行集合上的冗長循環。

這裏有CurrentRow屬性。

if(dgvDetials.CurrentRow != null) 
{ 
    // Reference the current row 
    DataGridViewRow row = dgvDetials.CurrentRow; 

    PartCode = row.Cells[0].Value.ToString(); 
    ..... 
    row.Cells[2].Value = 0; 
    row.Cells[5].Value = qtyInspCount; 
    row.Cells[3].Value = scrapCount; 
    row.Cells[4].Value = reworkCount; 
    ..... 
}