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

  • SEARCH
  • Home
  • 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 8035233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:15:10+00:00 2026-06-05T02:15:10+00:00

I need to count and present distinct/unique values in a dataGridView. I want to

  • 0

I need to count and present distinct/unique values in a dataGridView.
I want to present it like this, and this code works just fine with lists.

        List<string> aryIDs = new List<string>();
        aryIDs.Add("1234");
        aryIDs.Add("4321");
        aryIDs.Add("3214");
        aryIDs.Add("1234");
        aryIDs.Add("4321");
        aryIDs.Add("1234");

        var result= aryIDs.GroupBy(id => id).OrderByDescending(id => id.Count()).Select(g => new { Id = g.Key, Count = g.Count() });

But when I try to use the same approach on a column in dataGridView I get an error saying that groupBy cannot be used on my dataGridView.

        DataGridView dataGridView1= new DataGridView();
        dataGridView1.Columns.Add("nr", "nr");
        string[] row1 = new string[] { "1234" };
        string[] row2 = new string[] { "4321" };
        string[] row3 = new string[] { "3214" };
        string[] row4 = new string[] { "1234" };
        string[] row5 = new string[] { "4321" };
        string[] row6 = new string[] { "1234" };

        object[] rows = new object[] { row1, row2, row3, row4, row5, row6 };

        foreach (string[] rowArray in rows)
        {
            dataGridView1.Rows.Add(rowArray);
        }

        var result = dataGridView1.GroupBy(id => id).OrderByDescending(id => id.Count()).Select(g => new { Id = g.Key, Count = g.Count() });

So my question is, how do I adapt this linq syntax to work with a column in dataGridView? If possible, i dont want to use lists at all.

  • 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-06-05T02:15:11+00:00Added an answer on June 5, 2026 at 2:15 am

    This will work for your example:

    var result = dataGridView1.Rows.Cast<DataGridViewRow>()
        .Where(r => r.Cells[0].Value != null)
        .Select (r => r.Cells[0].Value)
        .GroupBy(id => id)
            .OrderByDescending(id => id.Count()) 
            .Select(g => new { Id = g.Key, Count = g.Count() });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to count all distinct values from table LDS where status = 'ok'
I have a table and I need to count all unique users on it.
I need to do some searching in the filesystem and would like to present
I need to count number of repeating and position where they repeat of all
I need to count the occurrences of these characters (most of them must be
I need to count the total number of instances in which a 10-digit number
I need to count the no of rows which is returned by the following
I've got some tests that need to count the number of warnings raised by
I have a a large text file (over 70mb) and need to count the
I have an array in jQuery, and I need to count the number of

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.