【发布时间】:2019-01-21 00:24:29
【问题描述】:
我已经在DataGridView 中放入了一些值,并希望将其列的总和得到一些TextBoxes。我希望当我将数据输入到DataGridView 时,这些特定列的总和应该自动放入相关的文本框。
【问题讨论】:
-
到目前为止你尝试过什么?
标签: c# winforms datagridview
我已经在DataGridView 中放入了一些值,并希望将其列的总和得到一些TextBoxes。我希望当我将数据输入到DataGridView 时,这些特定列的总和应该自动放入相关的文本框。
【问题讨论】:
标签: c# winforms datagridview
您可以在DataGridView 上订阅CellValueChanged 事件并在那里进行计算。
下面是它的示例:
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
// Determine what column has changed
var colIndex = e.ColumnIndex;
var sum = 0;
foreach (DataGridViewRow row in dataGridView1.Rows)
// Be aware of what numbers you have in your column!!
// Then cast it appropriately
sum += (int)row.Cells[colIndex].Value;
textBox1.Text = sum.ToString();
}
注意:我使用了由设计器生成的控件的默认名称。将它们相应地更改为您的名字。
【讨论】: