Freigeben über


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.

Comments

  • Anonymous
    March 23, 2005
    there is still one problem using DataGridView/BindingSource, when I Bind textBox with BindingSource. for example if we have such binding:<br><br>bindingSource.DataSource = dataSet;<br>bindingSource.DataMember = &quot;Customers&quot;<br>textBox.DataBindings.Add(&quot;Text&quot;, bindingSource, &quot;Customers.NAME&quot;);<br><br>textBox shows everything ok and we can also change the value of current row, but when doig this dataSet is not getting the changeg status and we can't determine whether dataset was changed - ((DataSet)bindingSource.DataSource).HasChanges() returns false instead of true<br><br>if you are known with this problem it will be good to hear how to solve this

  • Anonymous
    August 14, 2005
    You will need to call BindingSource.EndEdit() in this case. Changing the Position of the BindingSource will also do this.

    Regards,
    -Dinesh

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

  • Anonymous
    October 19, 2010
    how can i bind data from datagridview to textbox