DataGridView cell edit end event

.net, c#, datagridview, winforms

Solution

Use `CellEndEdit` event to update your total value:

private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    int total = 0;

    foreach (DataGridViewRow row in dataGridView.Rows)            
        total += (int)row.Cells[columnTotal.Index].Value;

    totalTextBox.Text = total.ToString();           
}

Problem

I have a DataGridView in which i have a column 'total'. DataGridView is editable true. below the grid view i have textbox in which i want the total of the 'total' column of grid. What i did is, when user enters into the total column in grid, it reflects into the total text field belo the grid view. to display total in the textfield, i added the total column of grid view. But the problem is, if first time i enter into the total column of grid view, it immediately reflects into the textfield below. but if i edit the same value in total column of DataGridView, the textfield below the grid adds it with previous value where i want the new edited value in the textfield. How to fix this?Following is the code:- ``` private void grdCaret_CellEndEdit(object sender, DataGridViewCellEventArgs e) { try { string value = grdCaret.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(); if (e.ColumnIndex == 1) { // int val = int.Parse(value); // quantity = val; // ekundag = ekundag + quantity; //tbTotDag_cr.Text =ekundag.ToString(); int quantity = 0; foreach (DataGridViewRow row in grdCaret.Rows) quantity +=(int) grdCaret.Rows[e.RowIndex].Cells[1].Value.ToString(); //quantity +=(int) row.Cells[1].Value; tbTotDag_cr.Text = quantity.ToString(); } if (e.ColumnIndex == 2) { float val = float.Parse(value); total = val; ekunrakam = ekunrakam + total; tbTotPrice_cr.Text = ekunrakam.ToString(); } grdCaret.Columns[3].ReadOnly = false; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } ```

Original source