Search code examples
c#datagridviewbindingsource

BindingSource - what are the advantages of using BindingSource


What gives me using something like this:

DataGridView dgvDocuments = new DataGridView();
BindingSource bindingSource = new BindingSource();
DataTable dtDocuments;

dtDocuments = MsSQL.GetDocuments(dtpOd.Value, dtpDo.Value);
bindingSource.DataSource = dtDocuments;
dgvDocuments.DataSource = bindingSource;

instead of this:

DataGridView dgvDocuments = new DataGridView();
DataTable dtDocuments;

dtDocuments = MsSQL.GetDocuments(dtpOd.Value, dtpDo.Value);
dgvDocuments.DataSource = dtDocuments;

Solution

  • BindingSource have many benefits, below some of them

    1) When you bind data to any control with bindingsource it will take effect both sides. Any changes made to datasource effects to control and any change to control effects datasource. You don't need take value from control and assign to datasource again

    2) You can apply filter to datasource with bindingsource

    3) You can work with one datasource binded to many controls. For Example you have table Fruits, and you bind this table to 2 DataGridView to show Aplles and Peaches seperately. With bindingsource Filter property you can show Apples and Peaches seperately.

    4) You can do Searching, Sorting, Editing, Filtering with bindingsource

    You can not see bindingsource benefit on basic lists, but there is more than basic list you will see how bindingsource is usefull.

    You can get more informtaion Here