BindingSource and BindingList Of T - DataBinding made simple!

In Whidbey we have new classes: BindingSource and BindingList<T> which should make DataBinding really easy even when binding to business objects. Main advantages are:

- BindingSource hooks on to property changes of the current item and fires ListChanged events when the underlying event changes

- BindingList<T> is the new generic implementation of IBindingList which fires ListChanged event when items are added/removed/inserted/etc. from the list. bindingSource hooks on to these events and is thus “aware” of these changes and can notify controls bound thos this BindingSource.

Let’s take some common case scenarios and see hpow BindingSource and BindingList<T> can be used for these:

1) DataGridView bound to DataTable

            DataGridView dgv = new DataGridView();

            BindingSource bs = new BindingSource();

            DataTable dt = GetDataTableFromSomeWhere(); // Gets the DataTable

            bs.DataSource = dt;

            dgv.DataSource = bs;

That’s it. You are done! DataGridView is now bound to the DataTable.

Ofcourse you can bind the DataGridView to the DataTable directly and bypass the BindingSource, but BindingSource has certain advantages:

- It exposes properties to Sort the list, Filter the list, etc. which would other wise be a pain to do. (i.e. if you bind the DataGridView to the DataTable directly then to Sort the DataTable you need to know that DataTable is an IListSource which knows the underlying list which is a DataView and a DataView can be sorted, filtered, etc.).

- If you have to set up master/child views then BindingSource does a great job of doing this (more details in my previous post)

- Changes to the DataTable is hidden (also in my previous post)

2) DataGridView bound to business objects

Say you want to bind the DataGridView to a list of Customers. Customer is a class you have created and has properties like ID, Name, etc. To do this you can simple do:

            DataGridView dgv = new DataGridView();

            BindingSource bs = new BindingSource();

            BindingList<Customer> bList = new BindingList<Customer>();

           

            // Fill bList with Customers

            bs.DataSource = bList;

            dgv.DataSource = bs;

That’s it. You are done! DataGridView will show all the Customers in the BindingList and display columns for each of the properties on Customer i.e. it will have columns for ID, Name, etc. Also, advantages of BindingList<T> are that as you add, remove, insert Customers into this BindingList the DataGridView will show up the changes and correspondingly add, remove and insert rows!

3) TextBox bound to business objects

Say you want to bind a TextBox (or any other control) to a list of Customers and show up the Name of the Customer in the TextBox. You can simply do:

            TextBox tb = new TextBox();

            BindingSource bs = new BindingSource();

            BindingList<Customer> bList = new BindingList<Customer>();

           

            // Fill bList with Customers

            bs.DataSource = bList;

            tb.DataBindings.Add(“Text”, bList, “Name”);

That’s it. This will bind the TextBox to the Name property of the Customer in the list. Initially it will show the Name of the first customer in the list. As you change the Position property of the BindingSource, it will show the Name of the “current” Customer (current item is whatever the Position points to).

You can do a similar thing by using DataTable instead and bind to one of the Columns in the DataTable.

4) ComboBox bound to business objects

Say you want to bind a ComboBox (or one of the list controls like ListView, etc) to a list of Customers and show up a list of Names of the Customers in the list. You can simply do:

            ComboBox cb = new ComboBox();

            BindingSource bs = new BindingSource();

            BindingList<Customer> bList = new BindingList<Customer>();

           

            // Fill bList with Customers

            bs.DataSource = bList;

            cb.DataSource = bList;

            cb.DisplayMember = “Name”;

That’s it. This will bind the ComboBox and it’s items will be the Name of all the Customers in the BindingList.

You can do a similar thing by using DataTable instead and bind to one of the Columns in the DataTable.

In all these 4 cases you get all the advantages of BindingSource and BindingList<T> mentioned above. Simple isn’t it? Happy Binding! J