Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7619241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:38:13+00:00 2026-05-31T03:38:13+00:00

how to do it? I’ve fount this code in How to Count Duplicates in

  • 0

how to do it?

I’ve fount this code in How to Count Duplicates in List with LINQ :

 var list = new List<string> { "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);
        }

But how to modify it to count duplicates in datagridview? For example datagridview1[7,i] where i is number of rows in datagriview.

EDIT

Now my code is looking like that:

      var list = dataGridView1.Rows.OfType<DataGridViewRow>()
       .GroupBy(x => x.Cells["TestValues"].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[7, x].Value.ToString();

            MessageBox.Show("Value: " + x.Value + " Count: " + x.Count +"Rows: " );

        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:38:14+00:00Added an answer on May 31, 2026 at 3:38 am

    Something like this should work:

    var list = myDataGridView.Rows.OfType<DataGridViewRow>()
               .Select(x => x.Cells["MYCOLUMN"].Value.ToString());
    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 };
    

    where "MYCOLUMN" is the name of the column that you want, or, alternatively, you can pass the column index.

    EDIT :

    this code returns a list of items that contains also the list of rows with the duplications:

    var q = myDataGridView.Rows.OfType<DataGridViewRow>()
            .GroupBy(x => x.Cells["MYCOLUMN"].Value.ToString())
            .Select(g => new {Value=g.Key, Count=g.Count(), Rows=g.ToList()})
            .OrderByDescending(x => x.Count);
    

    so if you have 5 rows e.g. :

    ID     MYCOLUMN
     0         A
     1         B
     2         C
     3         A
     4         B   
    

    q will contain 3 elements:

     Key="A", Count=2, Rows={ [0 - A] [3 - A]}
     Key="B", Count=2, Rows={ [1 - B] [4 - B]}
     Key="C", Count=1, Rows={ [2 - C] }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.