blubberbernd
blubberbernd

Reputation: 3701

C#, BindingList, BindingSource, sorting a ListBox?

I already googled for an hour now and didn't find a working answer.

I have a myClass with different members such as a string Name and some int Numbers. Then I have a myList of type List and then I have a Listbox control.

I can easily display the List in the Listbox by using DisplayMember = "Name" and DataSource = new BindingList<myClass>(myList.Values).

But what I want is to make it sortable by the different Members of my class. I already tried creating a BindingSource of my List and pass it to the Listbox, but then the only entry in my Listbox is "(Enumeration)" (the type of the bindinglist, but not its members).

What do I need to display a simple List in a simple Listbox control AND make it sortable by the members of T?

Upvotes: 0

Views: 3550

Answers (1)

Alex J
Alex J

Reputation: 10205

BindingList doesn't implement sorting by itself, although it offers you that capability should you choose to implement it. You can inherit from BindingList and start from here. Keep in mind that sort operations probably shouldn't modify the original list.

There are existing implementations, like the BindingListView that will also give you what you need.

However, the easiest way I've found is to use a DataTable populated with the members of the class. This will let you leverage the DataView for free which already supports sorting, filtering, and all the nice things.

Upvotes: 1

Related Questions