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 3695620
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:38:23+00:00 2026-05-19T04:38:23+00:00

A relatively simple question. I have a datagridview, which all it does is displays

  • 0

A relatively simple question. I have a datagridview, which all it does is displays statistics. There is no editing/adding/deleting of rows. The datagridview is bound to a List. All I want to achieve is to have the user be able to sort the columns.

class Market
{
    public int Location {get;set;}
    public float Value {get;set;}
    //...
}
class City
{
    public String Name {get;set;}
    //...
}

List<Market> _markets;
List<City> _cities;
//Lists are populated.

dataGridView1.DataSource = _markets.Select(market => 
    new { _cities[market.Location].Name, market.Value}).ToList();

As expected, the columns are not sortable, but the information that is displayed is what is wanted. My question is how to make the DataGridView sort based on the column type with the least complicated and least amount of code, as the code will be used multiple times throughout.

This application used to use a DataBase that had views. These views then populated the DataGridViews. The views are still around, so a possible solution could be:

DataBase.ViewMarketValue temp = new DataBase.ViewMarketValue()

_markets.ForEach(market => temp.AddViewMarketValueRow(_cities[market.Location].Name, market.Value);
dataGridView1.DataSource = temp;

This results in the desired: a datagridview that has all the information and it is sortable. The only problem is that it seems wrong to use views in this aspect. So what should I do?

  • 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-19T04:38:24+00:00Added an answer on May 19, 2026 at 4:38 am

    In order to be able to sort the data automatically in the DataGridView, you need a collection that implements IBindingListView. In the BCL, the only classes that implement this interface are DataView and BindingSource (but the latter only supports sorting if the underlying datasource supports it too).

    So, you have several options:

    • create a DataTable to hold the data, and bind it to the DataGridView (it will actually bind to the DefaultView of the DataTable)
    • create your own collection class that implements IBindingListView
    • use an existing implementation, like the AdvancedList<T> class posted by Marc Gravell in this post. You will also need to add a constructor to build the list from the result of your query:

      public AdvancedList(IEnumerable<T> collection)
      {
          foreach (var item in collection)
          {
              Add(item);
          }
      }
      

    Since the result of your query is an anonymous type, you won’t be able to call the constructor directly. The easiest way to work around the issue is to take advantage of type inference, by creating a generic method that will create the list. For convenience, you can create it as an extension method:

    public static AdvancedList<T> ToAdvancedList<T>(this IEnumerable<T> source)
    {
        return new AdvancedList<T>(source);
    }
    

    You can then use it like that:

    dataGridView1.DataSource = _markets.Select(market => 
        new { _cities[market.Location].Name, market.Value}).ToAdvancedList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a relatively simple form which asks a variety of questions. One of
I'm sure this is a relatively simple question, and there must be a good
Is there a relatively simple way to change all the data in a JTable?
Is there a relatively simple way in nant, without writing a custom task, to
I have a relatively simple site that I'm working up for an intranet environment.
We have are relatively simple Reporting Services report that our users commonly export to
I have a relatively simple select statement in a VB6 program that I have
Given a relatively simple CSS: div { width: 150px; } <div> 12333-2333-233-23339392-332332323 </div> How
My program generates relatively simple PDF documents on request, but I'm having trouble with
I am building a relatively simple Django application and apart from the main page

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.