繁体   English   中英

按下键盘上的删除键时,无法自动更新文本框的总值

[英]can't automatically update the total value of the textbox when pressing the delete key on the keyboard

我想总结在第4列中自动输入的所有值并将其显示在文本框中。 我用这个代码:

private void totalAll()
{
    int a = 0, b = 0;
    for (a = 0; a < dataGridView1.Rows.Count; ++a)
    {
        b += Convert.ToInt32(dataGridView1.Rows[a].Cells[3].Value);
    }
    textBox1.Text = b.ToString();
}

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    foreach (DataGridViewRow row in dataGridView1.Rows)
        totalAll();           
}

一切正常,但是当我按下键盘上的删除键时,文本框的总值不会自动更新。 它的价值不会减少。

在删除之前

在此输入图像描述

从数据网格视图中删除行时,应重新计算总值。

用户删除行时应该监听事件你应该在InitializedComponent();之后添加它InitializedComponent(); 在您的表单构造函数中。

dataGridView1.UserDeletedRow += (s, e) => totalAll();

尝试订阅dataGridView事件RowsRemoved。 我没有测试这个,但应该是这样的:

dataGridView1.RowsRemoved += (s,e) => totalAll();

顺便说一句,我认为你可以删除dataGridView1_CellEndEdit方法上的foreach,似乎什么都不做

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM