我能够得到整个列的总和,但在我的数据网格总和中仅获得选定的项目时遇到了问题。以下是我到目前为止所掌握的:
private void button5_Click(object sender, EventArgs e)
{
decimal sum = csv_datagridview.Rows.OfType<DataGridViewRow>()
.Sum(row => Convert.ToDecimal(row.Cells[7].Value));
MessageBox.Show("Total is: " + sum.ToString());
}
发布于 2017-01-13 00:50:56
为什么不在Rows
上使用SelectedRows
呢?DataGridView的SelectedRows属性将为您提供选定的行,您可以遍历这些行并获得总和,请参见以下代码:
decimal sum = csv_datagridview.SelectedRows.OfType<DataGridViewRow>()
.Sum(row => Convert.ToDecimal(row.Cells[7].Value));
https://stackoverflow.com/questions/41625658
复制相似问题