Share via


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

Comments

  • Anonymous
    June 03, 2005
    There is a new set of helper classes implemented in the Windows Forms 2.0 that I am currently attempting to work with named BindingSource and BindingList. Basically they simplify the process of data binding objects to controls.

    Unfortunately, most tutor

  • Anonymous
    March 06, 2006
    If you put a blank Grid on a form, what do you have to do to it to get this to work.  I don't see anything happen.  TIA

  • Anonymous
    August 22, 2006
    Hi,

    I want to spin up my own version of a datatable to allow me to do virtual loading of the data.

    Therefor I have a list of rows with a list of fields.

    So I've got

    List of Rows
      List of Fields
         Field1
         Field2

    etc.

    How can I make my mode bindable ? Do I need to inherit Ilist for both lists, and what do I do with each field which will have name, value etc ??

    Sorry if I'm being dumb !

    LJ

  • Anonymous
    March 13, 2008
    I had an situation like this with "TextBox bound to business objects" TextBox is  handling Validating Event for some error handling. (null/empty string not allowed) So when i bind the "DataSource" to business object in genral, it clears the TextBox content. (but during initialization this TextBox initialized with "XYZ" data) i.e bindingSource1.DataSource = typeof(classX) So i want to ignore this case (also handle the Validating Event which deals at UI level). Anybody faced this situation and how it is resolved. I have tried with bindingSource1.Clear() bindingSource1.RemoveCurrent(). But all this have impact with TextBox.

  • Anonymous
    May 01, 2008
    PingBack from http://ramsonit.wordpress.com/2008/05/01/databinding-basics/

  • Anonymous
    January 18, 2009
    PingBack from http://www.keyongtech.com/680617-custom-object-bound-to-combobox

  • Anonymous
    January 21, 2009
    PingBack from http://www.keyongtech.com/658001-need-help-creating-list-of

  • Anonymous
    May 28, 2009
    PingBack from http://paidsurveyshub.info/story.php?title=dc-s-net-client-blogs-bindingsource-and-bindinglist-of-t

  • Anonymous
    June 13, 2009
    PingBack from http://firepitidea.info/story.php?id=542

  • Anonymous
    January 07, 2014
    Ok this is the simple BindingList - now can anyone tell me how to use BindingList of myObject[] where MyObject has the IList ? - I can bind to MyObject but that is one object , I want to Bind to MyObjects[] - please explain how to do this.