开发人员技术 | Windows 窗体
一组用于开发图形用户界面的 .NET Framework 托管库。
我有 dataGridView1 包含两列,Col1 和 Col2,都包含重复值。
Column1 | Column2
2515 | 1105
1105 | 2515
3800 | 2208
2515 | 1105
2508 | 3800
我需要通过仅选择以 25 开头的值来计算两列中值的重复次数,然后在单击按钮后在 dataGridView2 中显示结果,该结果由列组成:Id、Value 和 Rerepeat。
我尝试了以下方法,但错过了仅选择和计算以 25 开头的值的条件:
var s1 = dt.AsEnumerable().Select(r => r.Field<string>("Column1")).ToList();
var s2 = dt.AsEnumerable().Select(r => r.Field<string>("Column2")).ToList();
List<string> list = new List<string>();
list.AddRange(s1);
list.AddRange(s2);
var result = list.GroupBy(x => x)
.Select(g => new {
Value = g.Key, Count = g.Count()
})
.OrderByDescending(x => x.Count);
int count = 1;
dataGridView2.Columns.Add("Id", "");
dataGridView2.Columns.Add("Value", "");
dataGridView2.Columns.Add("Repetition", "");
foreach (var item in result)
{
dataGridView2.Rows.Add(count, item.Value, item.Count);
count++;
}
如何计算 datagridview1 中以 25** 开头的值并在 dataGridView2 中显示它们?
感谢您的帮助!
Note:此问题总结整理于:Count repetition of data starting with specific value in a DataGridView
一组用于开发图形用户界面的 .NET Framework 托管库。
问题作者接受的答案
欢迎来到Microsoft问答,你可以尝试以下代码来筛选值以 25 开头的条件。
foreach (var item in result)
{
if(item.Value.StartsWith("25"))
{
dataGridView2.Rows.Add(count, item.Value, item.Count);
count++;
}
}
结果:
如果答案是正确的,请点击“接受答案”并点赞。 如果您对此答案还有其他疑问,请点击“评论”。
注意:如果您想接收相关电子邮件,请按照我们的文档中的步骤启用电子邮件通知 此线程的通知。