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.
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>:
ApplySortCore changes to:
Now, in the derived class one have to implement comparers for each sortable property:
This variant requires a little bit more code but, if performance is an issue, I think it worths the effort.