DataBinding – DataGridView/BindingSource

Many folks I know find DataBinding a complex thing and try to stay away from it. Here is a quick overview of DataBinding and how it relates to DataGridView (a BIG feature in Whidbey!) and BindingSource (will make life simple!).

Before Whidbey a typical data bound app would have an Adapter (that would talk to the back end), a DataSet (which would be filled up by the Adapter) and some controls (like DataGrid, TextBox, ComboBox, etc.) which would talk to a DataSet. This would work most of the time but there were certain problems:

1. Folks would bind say, a DataGrid to a DataTable. Then somewhere the DataTable would be updated i.e. say you contacted some WebService or whatever else you want, to get the new DataTable. Expectation was that databinding should still work. Well, this would not work since the DataTable has now been changed. A snippet of this is:

DataGrid dg = new DataGrid();

DataTable dt = MyWebService.GetDataTable();

dg.DataSource = dt; // works fine

dt = MyWebService.GetDataTable(); // breaks since this is a new datatable

2. Other problem was with setting up Master/Child views. Say we have a DataSet that has two tables: CustomerTable called “Customers” and OrderTable called “Orders” and a relation called “CustOrders”. Typically to set up Master/Child DataGrids,

The correct ways to do this would be (DataSource for both grids should be same):

Correct Way 1:

           

dgMaster.DataSource = ds;

dgMaster.DataMember = “Customers”;

dgChild.DataSource = ds;

dgChild.DataMember = “Customers.CustOrders”;

Correct Way 2:

           

dgMaster.DataSource = CustomerTable;

dgChild.DataSource = CustomerTable;

dgChild.DataMember = “CustOrders”;

Other ways to do this would be incorrect, such as (DataSource for grids are different which would result in Master/Details not working correctly):

Incorrect Way 1:

           

dgMaster.DataSource = ds;

dgMaster.DataMember = “Customers”;

dgChild.DataSource = CustomerTable;

dgChild.DataMember = “CustOrders”;

Incorrect Way 2:

           

dgMaster.DataSource = CustomerTable;

dgChild.DataSource = ds;

dgChild.DataMember = “Customers.CustOrders”;

To mitigate these and other issues, we have introduced the new component BindingSource. BindingSource has DataSource and DataMember properties that you can set and then bind to the BindingSource. You can also change the DataSource of BindingSource and the binding will work fine.

e.g. Say you are using the new DataGridView control of Whidbey and want to show the Customers table (using BindingSource in this case is an overkill, but it demonstrates how to use the BindingSource):

            DataGridView dgv = new DataGridView();

            BindingSource bs = new BindingSource();

            bs.DataSource = CustomerTable;

            dgv.DataSource = bs;

To set up Master/Child views, you can do the following:

            bsMaster.DataSource = ds;

            bsMaster.DataMember = “Customers”;

            bsChild.DataSource = bsMaster; // Note: DataSource is the master BindingSource

            bsChild.DataMember = “CustOrders”; // Note: DataMember is the relation

            dgvMaster = bsMaster;

            dgvChild = bsChild;

This hooks up the master/child views correctly. J

Please let me know if you have any comments or questions.