我怎样计算总和?dataGridView列中的值

本文关键字:dataGridView 计算 | 更新日期: 2023-09-27 18:05:37

如何计算存储在dataGridView列上的整数和浮点值的总和,单击按钮并将显示存储在文本框中?

我怎样计算总和?dataGridView列中的值

int sum = 0;
foreach (DataGridViewRow row in dataGridView.Rows)
{
    //here 0 is the index of columns you want to get values from 
    // you can also use column name instead of index
    sum += (int) row.Cells[0].Value;
}
//for showing the user message box
MessageBox.Show("Sum of all the values is {0}", sum.ToString());