Wednesday, November 23, 2005

Fun with BindingList(of T) and Bindable, Sortable Collections of Domain Objects

One of the things I really wanted to play with in VB/VS 2005 was data binding with domain objects (i.e. my own business classes and not a dataset/table).

I’m a big fan of using domain objects, where they make sense. I don’t believe in going overboard and requiring/making everything a domain object though. DataSets/Tables are pretty cool now and sure make some things very easy.

That said, I don’t want to have to coerce my perfectly good domain objects into datatables just so I can bind them to a DataGridView.

I looked at implementing IBindingList and what a nightmare. Next…

.Net 2.0 introduces the BindingList(of T) class. This is a great base class to inherit from for creating custom, bindable domain object collections.

With a little help from Sorting the Unsortable Collection, by Rockford Lhotka and Custom Data Binding, Part 2, by Michael Weinhardt, I now have a fully working datasource bindable collection of domain objects. This includes sorting, editing, adding, etc, etc.

To get sorting working, I inherited from BindingList(of T), overrode and implemented two Subs and four Properties. And that’s about it! If you’ve ever looked at supporting sorting with IBindingList you’ll know why I’m so excited.

Michael gets the credit for performing the real magic. His implementation of the PropertyComparer made my work easy.

Next week or so, I’m going to try and write this up in more detail and post it to Code Project.

Update #1 1/9/2005 @ 4:13 PM (PST):
I finally got off my butt and posted a sample of the work I mention above...

Check out this post for more information, or see the GotDotNet User Sample, VB.Net Binding List With Sorting Example.

Update #2 4/28/2008 @ 2:45 PM (PDT):
GotDotNet is gone, along with my sample code... sigh... :(

But luckilyI had a backup copy.

For the time being I'm going to post it to a "readable by everyone" SkyDrive folder, Fun With Bindling List Source

Thank you Anna for reminding me to repost this source.


David said...

Did you ever get a chance to write up your BindingList class? I'm working on the same issue, and it would be nice to see what someone else has done!

Greg said...

No I haven't...

But I think I'll have some time as soon as this week to quickly write something up...

(Yeah, I know... Promises, promises... ;)

Greg said...

I just quickly knocked out an example project which shows how I'm using BindingList with sorting, etc...

If you shoot me an email, I'll send it to you, if you're still interested.

gduncan411 [at] hotmail[thedot]com said...

Please excuse my ignorance, but how do you explicitly invoke the Sort from code when it is not bound to a datagrid?

Gregorius said...

hey Greg

I just wanted to say thank you for putting this together... I've implemented it on my project, and it works like a dream. I actually created a generic class out of it (GenericBindingList) that i can use to store collections of any types of objects, bind directly to DGV, and then it'll sort them, as well as allow inline editing as its bound directly to the control.

Brilliant stuff :)

Love your work..

Alexandre said...

Hi greg i would be very interested in your generic bindinglist class for a little work i'm doing right now. Seems nice and would be greatly appreciated right now.

djsinae (at)


Greg said...

Email sent with the URL for the public SkyDrive folder.

It's the same one in the Update #2...


Sebastian said...

the links are dead yet :(

Greg Duncan said...

@Sebastian Thanks for the heads up. Looks like the SkyDrive URLs changed. Link updated (to