c# - 计算具有相同值的 DataGridView 的单元格并显示结果
问题描述
我有一个包含 9 列的 DataGridview,最后一列显示“已完成”。
此列的值为:Completed
, In-Repair
, Build In Progress
, Not Started
。
我想计算有多少Completed
个单元格并将结果显示在标签中,例如:“23 Rows Show Completed”。
我已经尝试了我所知道的一切,但无法让它发挥作用。
我试过使用System.Linq
,for each
但我无法让它工作。
int xCount = dataGridView1.Rows
.Cast<DataGridViewRow>()
.Select(row => row.Cells["Finished"].Value.ToString())
.Count(s => s == "Completed");
lbl3.Text = xCount.ToString();
解决方案
尝试使用索引
int xCount = dataGridView1.Rows
.Cast<DataGridViewRow>()
.Select(row => row.Cells[8].Value.ToString() == "Completed").Count();
lbl3.Text = xCount.ToString();