title: How to Count Duplicates in Datagridview in C#
body: <p>how to do it? </p>
  
  <p>I've fount this code in <a href="https://stackoverflow.com/questions/454601/how-to-count-duplicates-in-list-with-linq">How to Count Duplicates in List with LINQ</a> :</p>
  
  <pre><code> var list = new List&lt;string&gt; { "a", "b", "a", "c", "a", "b" };
  
          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)
          {
              MessageBox.Show("Value: " + x.Value + " Count: " + x.Count);
          }
  </code></pre>
  
  <p>But how to modify it to count duplicates in datagridview? For example datagridview1[7,i] where i is number of rows in datagriview.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Now my code is looking like that: </p>
  
  <pre><code>      var list = dataGridView1.Rows.OfType&lt;DataGridViewRow&gt;()
         .GroupBy(x =&gt; x.Cells["TestValues"].Value)
         .Select(g =&gt; new { Value = g.Key, Count = g.Count(), Rows = g.ToList() })
         .OrderByDescending(x =&gt; 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[7, x].Value.ToString();
  
              MessageBox.Show("Value: " + x.Value + " Count: " + x.Count +"Rows: " );
  
          }
  </code></pre>