Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
https://i.postimg.cc/6qKk002q/Capture.png[^]

What I have tried:

var list = dataGridView1.Rows.OfType<DataGridViewRow>().GroupBy(x => x.Cells["Agent Name"]
          .Value).Select(g => new { Value = g.Key, Count = g.Count(), Rows = g.ToList() }).OrderByDescending(x => x.Count);



           var q = from x in list
                   group x by x into g
                   let count = g.Count()
                   orderby count descending
                   select new { Value = g.Key, Count = count };



           foreach (var x in q)
           {
               dataGridView1.Rows.Add(x.Value );


               //dataGridView2.Rows.Add("Agent" + x.Value + "Scour" + x.Count + "Rows");
               //  dataGridView2.Rows.Add(x.Value ,x.Count);

               //
           }
Posted
Updated 18-Nov-19 8:54am

1 solution

You're grouping the source data twice. As a result, every row will end up with a count of 1.

You're also adding the grouped rows to the wrong grid, and not adding the count.

Change the code so that it only groups once, and add the name and count to the second grid:
C#
var list = dataGridView1.Rows.Cast<DataGridViewRow>()
    .GroupBy(r => r.Cells["Agent Name"].Value, (name, rows) => new { name, Count = rows.Count() })
    .OrderByDescending(g => g.Count);

foreach (var g in list)
{
    dataGridView2.Rows.Add(x.name, x.Count);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900