0

对于每个语句,我需要一些帮助,基本上,当用户编辑单元格中的值时,我的 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;
            }
4

2 回答 2

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; 
    .....
}
于 2012-08-23T12:52:18.913 回答
1

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

GridViewRow datRow = SomeGridView.SelectedRow;
于 2012-08-23T13:37:45.860 回答