4
votes

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());
    }
}
3
Could you provide your cell edit event handler code here? - Sergey Berezovskiy
How are you running the Totals, code please - V4Vendetta

3 Answers

6
votes

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();           
}
0
votes
private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
  if (dataGridView.Rows.Count > 0)
   {
     Double dobTotal = 0.00;
     if (dataGridView.Columns["colAmountPaid"].Name.ToString().Equals("colAmountPaid"))
      {
       for (int i = 0; i < dataGridView.Rows.Count; i++)
       {
        dobTotal += Double.Parse(dataGridView["colAmountPaid",i].EditedFormattedValue.ToString());
       }
       txtTotal.Text = dobTotal.ToString();
      }
     }
   }
0
votes
<code>Private Sub EndEdit(ByVal sender As System.Object, ByVal e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
        If DataGridView1.IsCurrentCellDirty Then
            DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
        End If
    End Sub

<code>
    Private Sub DataGridView1_TextChanged(ByVal sender As System.Object, ByVal e As 
  System.Windows.Forms.DataGridViewCellEventArgs) Handles 
                                 DataGridView1.CellValueChanged
        If e.RowIndex = -1 Then
            isdirty = True
        End If

//All code you want to perform on change event

<code>    End Sub
</code>