foreach语句c#

时间:2012-08-23 12:48:47

标签: c# datagrid foreach

我需要为每个语句提供一些帮助,基本上当用户在单元格中编辑一个值时会发生这种情况,我的foreach会将其应用于数据网格中的所有单元格并更改它们的全部值,我需要我的foreach通过迭代数据网格来工作的语句,但只更改已编辑的选定行,但是当它似乎在更新所有行的计数时,这应该只是当前行

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;
            }

2 个答案:

答案 0 :(得分:2)

您只需要更改当前行 然后你应该避免整个datagrid行集合上的冗长循环。

只有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; 
    .....
}

答案 1 :(得分:1)

您需要绑定到CellValueChanged事件或获取选定的GridViewRow。

GridViewRow datRow = SomeGridView.SelectedRow;