Sorry it took me so long to respond to your reply to my comment.
So we've established that this is an empty dataGridView that the user will populate by typing in values. This is why the dataGridView1_DataSourceChanged_1 event is
never fired. That event would only get fired if you did something like
dataGridView1.DataSource = someDataTable;
which you are not going to do on a grid that is populated entirely by the User.
The CellEndEdit is the correct event to hook on to, to get your grand total.
This works:
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
double cell1 = 0;
double cell2 = 0;
if (e.ColumnIndex != 0 && e.ColumnIndex != 2) return;
foreach (DataGridViewRow r in dataGridView1.Rows)
{
double c1 = 0;
double c2 = 0;
if (r.Cells[0].Value != null)
if (double.TryParse(r.Cells[0].Value.ToString(), out c1))
cell1 += c1;
if (r.Cells[2].Value != null)
if (double.TryParse(r.Cells[2].Value.ToString(), out c2))
cell2 += c2;
}
dataGridView1.Rows[e.RowIndex].Cells[6].Value = cell1 + cell2;
}
I've assumed you are only adding up values in columns 0 and 2 and the current row gets the current grand total, previous rows are not revisited to update the grand total.
The code you originally posted is working fine for me, except it is calculating the
product (cell1 multiplied by cell2) per
row