计算 DataGridView 中以特定值开始的数据重复次数
我的 dataGridView1
包含两列:Col1 和 Col2,它们都包含重复的值。
Col1 | Col2 Id | Value| Repetition
=========== =======================
2515 | 1105 ---------- 1 | 2515 | 3
1105 | 2515 |button| 2 | 2508 | 1
3800 | 2208 ----------
2515 | 1105
2508 | 3800
我需要通过仅选择以 25 开头的值来计算两列中值的重复次数,然后在由 Columns: Id
组成的 dataGridView2
中显示结果、Value
和 Repetition
(单击按钮后)。
我尝试了以下逻辑,但我错过了仅选择、计数和显示以 25 开头的值的条件。这对所有值进行计数,并在第二个网格中显示两列中每个值的重复。
var q1 = dt.AsEnumerable().Select(r => r.Field<string>("Col1")).ToList();
var q2 = dt.AsEnumerable().Select(r => r.Field<string>("Col2")).ToList();
List<string> list = new List<string>();
list.AddRange(q1);
list.AddRange(q2);
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++;
}
如何计算以 25 开头的值并在 dataGridView2
中显示它们的重复(如上所示)?
I have dataGridView1
containing two columns, Col1 and Col2, which both contain duplicate values.
Col1 | Col2 Id | Value| Repetition
=========== =======================
2515 | 1105 ---------- 1 | 2515 | 3
1105 | 2515 |button| 2 | 2508 | 1
3800 | 2208 ----------
2515 | 1105
2508 | 3800
I need to count repetition of values from both columns by selecting only values starting with 25 then show the result in dataGridView2
which consists of Columns: Id
, Value
, and Repetition
, after clicking on button.
I tried the following logic but I miss the condition to select, count and show only values start with 25. this count all values and show in 2nd grid the repetition of every value in both columns.
var q1 = dt.AsEnumerable().Select(r => r.Field<string>("Col1")).ToList();
var q2 = dt.AsEnumerable().Select(r => r.Field<string>("Col2")).ToList();
List<string> list = new List<string>();
list.AddRange(q1);
list.AddRange(q2);
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++;
}
How can I count values start with 25 and show their repetition in dataGridView2
as shown above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我需要的条件是以下内容,它正常运行,如我所期望的
The condition I needed was the following and it works as I expected