Reputation: 1053
I need some help with a for each statement, basically what happens is when a user edits a value within a cell, my foreach will apply this to all cells within the datagrid and change the value of them all, i need my foreach statement to work by iterating through the datagrid but only change the selected row that has been edited, but at the minute it seems to be updating the count on all rows, this should be only the current row
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;
}
Upvotes: 0
Views: 844
Reputation: 1016
You need to bind to the CellValueChanged event or get the selected GridViewRow.
GridViewRow datRow = SomeGridView.SelectedRow;
Upvotes: 1
Reputation: 216243
You need to change only the current row.
Then you should avoid a lengthy loop on the whole datagrid rows collection.
There is the CurrentRow property just for this.
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;
.....
}
Upvotes: 2