我该如何计算总和。 dataGridView列中的值?

时间:2011-08-14 10:50:27

标签: c#

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

1 个答案:

答案 0 :(得分:2)

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