Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Sorting DataGridView
 

Sorting DataGridView

I bound an array to my DataGridView, like so:

Foo[] arr = getFoos();
dgw.DataSource = arr;

It works fine, but pressing the column headers does nothing. I want it to sort.

The Foo class only contains public properties of simple types (string, int, bool), which should be easy enough to sort, I should think. But no.

How can I fix this?

/David
pinkfloydhomer  Monday, July 14, 2008 6:38 PM

Actually just tested it and IComparible does not solve the problem, bummer!

I think the core issue is an array works because it supports IList, and IList has no provision for sorting. I'll search around to see if I can find an alternate solution.

Note I tried to force sorting and I got:

"DataGridView control must be bound to an IBindingList object to be sorted."

Hence I think you have to support IBindingList. Adding support for this is a huge amount of work, probably not worth it. Seems like there has to be a better way, hope someone else posts back with a solution, I'm stumped for now.

Matt Neerincx  Monday, July 14, 2008 8:38 PM

Hi David,

The SupportsSorting property IBindingList lets the collection specify whether it even supports sorting. If the collection supports sorting, then the ApplySort and RemoveSort methods let a control invoke or remove the sorting functionality provided by the collection. The ApplySort method takes two parameters: PropertyDescriptor and ListSortDirection.

·PropertyDescriptor identifies the property on which you want to sort

·ListSortDirection specifies the direction of a sort operation, whose value can be either Ascending or Descending.

Thus you can implement you own ApplySort method to sort on different propery. Here is a sample for your information.

By the way, IBindingList interface only supports sorting on a single property at a time. For multi-property sorts, you need to implement the IBindingListView interface.

Best regards.
Rong-Chun Zhang

Windows Forms General FAQs
Windows Forms Data Controls and Databinding FAQs

Rong-Chun Zhang  Friday, July 18, 2008 7:33 AM

I recall from fuzzy past memories off the top of my head if the type supports IComparible they should sort.

Hence try implementing IComparible in Foo class to see what happens.

Matt Neerincx  Monday, July 14, 2008 8:09 PM

Actually just tested it and IComparible does not solve the problem, bummer!

I think the core issue is an array works because it supports IList, and IList has no provision for sorting. I'll search around to see if I can find an alternate solution.

Note I tried to force sorting and I got:

"DataGridView control must be bound to an IBindingList object to be sorted."

Hence I think you have to support IBindingList. Adding support for this is a huge amount of work, probably not worth it. Seems like there has to be a better way, hope someone else posts back with a solution, I'm stumped for now.

Matt Neerincx  Monday, July 14, 2008 8:38 PM
I tried that also, and got the same result.

An IComparable does not really make sense. Because I don't want to just compare to Foos. I want to be able to compare two Foos with regards to _any_ of it's properties. If Foo is:

class Foo
{
public int i {get; set;}
public bool b {get; set;}
public string s {get; set;}
}

Having just Foo implement IComparable, how could I specify whether I wanted to sort by i, b or s? It doesn't make sense.

Having to implement IBindingList is really annoying. On the other hand, one should be able to to it once, and have it work for all future classes, for all public properties, using reflection.

Maybe there is a simpler approach for now? Something along the lines of subscribing to the DataGridView.SortCompare event. Still... It is not trivial to know which column was pressed, or to provide a short and simple solution that will work for all future properties.

It is really a shame! Binding DataGridView to some collection should really provide automatic sorting when all public properties of the objects in the collection support comparison already, as in my case. Should be simple enough...
pinkfloydhomer  Monday, July 14, 2008 8:50 PM

Probably simplest solution would be write some code to shovel thedata from yourarray into DataTable and back. Then bind to the DataTable and call it a day. You could do this with reflection as well, create columns in DataTable for each public property, etc... might not be too bad.

Matt Neerincx  Monday, July 14, 2008 8:59 PM

Hi David,

The SupportsSorting property IBindingList lets the collection specify whether it even supports sorting. If the collection supports sorting, then the ApplySort and RemoveSort methods let a control invoke or remove the sorting functionality provided by the collection. The ApplySort method takes two parameters: PropertyDescriptor and ListSortDirection.

·PropertyDescriptor identifies the property on which you want to sort

·ListSortDirection specifies the direction of a sort operation, whose value can be either Ascending or Descending.

Thus you can implement you own ApplySort method to sort on different propery. Here is a sample for your information.

By the way, IBindingList interface only supports sorting on a single property at a time. For multi-property sorts, you need to implement the IBindingListView interface.

Best regards.
Rong-Chun Zhang

Windows Forms General FAQs
Windows Forms Data Controls and Databinding FAQs

Rong-Chun Zhang  Friday, July 18, 2008 7:33 AM
I just don't understand why something like this isn't provided by the framework as long as we're talking about built-in simple types. The objects I am binding to the DataGridView only contains ints, strings and bools. Sorting those (and other simple types) should be provided by the framework somehow.


pinkfloydhomer  Friday, July 18, 2008 5:46 PM

Hi pinkfloydhomer,

Thanks for your feedback. Did you mean why the BindingList generic class doesn’t provide the sorting functionality? If so, I think the reason is that the BindingList generic class is used for generic collection that supports data binding. And it doesn’t know which object will be added to the collection. In your case, the business object is very simple; it only has very simple type as its properties, but if the business object has some complex type as its properties, the BindingList generic class may not know how to compare these properties.

Best regards.
Rong-Chun Zhang

Windows Forms General FAQs
Windows Forms Data Controls and Databinding FAQs

Rong-Chun Zhang  Tuesday, July 22, 2008 5:50 AM

You can use google to search for other answers

Custom Search

More Threads

• Data Not Saving
• keypress events trap on datagridviewcell
• Help: DataGridView.AutoSizeRows
• To set X & Y Cordinate Of Control Inside Datagridview
• Force DataGridView update
• Performance problem. How to clean up Dataset ?
• In Gridview how to give different colors to each cell. How to merge cells in middle, how to show Tool Tip,
• How to set a default value in a bound datatable
• setting datagridview.allowusertoaddrows = false throws Row Index out of range
• DataTable.Load() Throws InvalidOperationException