【发布时间】:2021-08-13 11:46:08
【问题描述】:
我使用这个 linq 查询对 datagridview 中的一些值进行分组和计算。
var Sums = dataGridView1.Rows.Cast<DataGridViewRow>()
.Where(row => row.Cells[8].Value != null)
.GroupBy(row => row.Cells[8].Value.ToString())
.Select(g => new { Gruppo = g.Key, Serie = g.Sum(row => Convert.ToInt32(row.Cells[2].Value)) });
如何在每组和总数之间添加百分比计算来改进这一点?
我想要的是,如果“Serie”列值的总和为 20(例如),在第三列中我看到 3(第一个单元格中的值)是 15% 等。
【问题讨论】:
-
对于我使用的数据源:dataGridView.DataSource = Sums.ToList();问题是,对于我用来计算百分比的任何代码,我得到的结果都是 0
-
@JohnG 我发现了问题。我将 linq 查询从 Convert.ToInt 更改为 Convert.ToDecimal 并得到正确的结果,然后我使用 dataGridView1.Columns["Percent"].DefaultCellStyle.Format = "0.00'%'";从您的答案中得出结果。谢谢!
标签: c# linq datagridview