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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:36:26+00:00 2026-05-12T05:36:26+00:00

I’m using a BindingList<T> in my Windows Forms that contains a list of IComparable<Contact>

  • 0

I’m using a BindingList<T> in my Windows Forms that contains a list of “IComparable<Contact>” Contact-objects. Now I’d like the user to be able to sort by any column displayed in the grid.

There is a way described on MSDN online which shows how to implement a custom collection based on BindingList<T> which allows sorting. But isn’t there a Sort-event or something that could be caught in the DataGridView (or, even nicer, on the BindingSource) to sort the underlying collection using custom code?

I don’t really like the way described by MSDN. The other way I could easily apply a LINQ query to the collection.

  • 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-12T05:36:26+00:00Added an answer on May 12, 2026 at 5:36 am

    I higly appreciate Matthias’ solution for its simplicity and beauty.

    However, while this gives excellent results for low data volumes, when working with large data volumes the performance is not so good, due to reflection.

    I ran a test with a collection of simple data objects, counting 100000 elements. Sorting by an integer type property took around 1 min. The implementation I’m going to further detail changed this to ~200ms.

    The basic idea is to benefit strongly typed comparison, while keeping the ApplySortCore method generic. The following replaces the generic comparison delegate with a call to a specific comparer, implemented in a derived class:

    New in SortableBindingList<T>:

    protected abstract Comparison<T> GetComparer(PropertyDescriptor prop);
    

    ApplySortCore changes to:

    protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
    {
        List<T> itemsList = (List<T>)this.Items;
        if (prop.PropertyType.GetInterface("IComparable") != null)
        {
            Comparison<T> comparer = GetComparer(prop);
            itemsList.Sort(comparer);
            if (direction == ListSortDirection.Descending)
            {
                itemsList.Reverse();
            }
        }
    
        isSortedValue = true;
        sortPropertyValue = prop;
        sortDirectionValue = direction;
    }
    

    Now, in the derived class one have to implement comparers for each sortable property:

    class MyBindingList:SortableBindingList<DataObject>
    {
            protected override Comparison<DataObject> GetComparer(PropertyDescriptor prop)
            {
                Comparison<DataObject> comparer;
                switch (prop.Name)
                {
                    case "MyIntProperty":
                        comparer = new Comparison<DataObject>(delegate(DataObject x, DataObject y)
                            {
                                if (x != null)
                                    if (y != null)
                                        return (x.MyIntProperty.CompareTo(y.MyIntProperty));
                                    else
                                        return 1;
                                else if (y != null)
                                    return -1;
                                else
                                    return 0;
                            });
                        break;
    
                        // Implement comparers for other sortable properties here.
                }
                return comparer;
            }
        }
    }
    

    This variant requires a little bit more code but, if performance is an issue, I think it worths the effort.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't

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.