SHEMA Valentin

SHEMA Valentin

  • NA
  • 32
  • 547

Count repetition of data starting with specific value in a DataGridVie

Apr 9 2022 6:32 PM

Hello

I have dataGridView1 containing two columns, Col1 and Col2, which both contain duplicate values.

Col1 | Col2                  Id | Value| Repetition
===========                 =======================
2515 | 1105    ----------     1 | 2515 | 4
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: IdValue, 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.

DataTable dt = READExcel(filePath); // data was displayed in dgv1 after importing from excel
dataGridView1.DataSource = dt;           
var q1 = dt.AsEnumerable().Select(r => r.Field("Col1")).ToList();
var q2 = dt.AsEnumerable().Select(r => r.Field("Col2")).ToList();
List list = new List();
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 dataGridView2as shown above

Please, Your ideas will be appreciated


Answers (1)