C# DataGridView 单元格编辑结束事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11028405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:10:38  来源:igfitidea点击:

DataGridView cell edit end event

c#.netwinformsdatagridview

提问by Harshali

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:-

我有一个 DataGridView,其中有一列“总计”。DataGridView 是可编辑的。在网格视图下方,我有文本框,在其中我想要网格的“总计”列的总数。我所做的是,当用户进入网格中的总计列时,它会反映到网格视图下的总计文本字段中。为了在文本字段中显示总计,我添加了网格视图的总计列。但问题是,如果我第一次进入网格视图的总列,它会立即反映到下面的文本字段中。但是如果我在 DataGridView 的总列中编辑相同的值,网格下方的文本字段会将其添加到以前的值中,我希望文本字段中的新编辑值。如何解决这个问题?以下是代码:-

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());
    }
}

回答by Sergey Berezovskiy

Use CellEndEditevent to update your total value:

使用CellEndEdit事件更新您的总价值:

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();           
}

回答by Asif

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();
      }
     }
   }

回答by Heemanshu Bhalla

<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>