BindingSource - what are the advantages of using BindingSource
Asked Answered
M

2

21

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;
Myungmyxedema answered 31/1, 2017 at 11:54 Comment(0)
S
13

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

Spatula answered 31/1, 2017 at 12:11 Comment(1)
Note that for #1 to work, the model has to implement INotifyPropertyChanged.Poussette
J
5

One of the advantages is that if you manipulate values in the DataGridView manually then the changes will be reflected in the underlying data. (EDIT: apparently this also works with normal DataSource binding.)

another advantage is that you get the possibility to add an entry to the underlying data (at least if it is a List) by clicking on the extra empty field and edit the values. This will add a new item without any additional code for you to write.

enter image description here

This Detailed Data Binding Tutorial may help to shed more light on the capabilities of data binding in general

EDIT:

One more difference is that manipulation of the underlying data, like adding an item to a List will not be reflected in the DataGridView even if assigning the DataSource property again which would work for example in a ComboBox. But a reassigning of a new instance of a BindingSource will do the trick.

So if you have a List of persons:

List<pers> list = new List<pers>();
BindingSource bs = new BindingSource();
bs.DataSource = perlist;
dataGridView1.DataSource = bs;

and later on want to add a new item to the list in the code, just create a new instance of BindingSource, reassign it to the DataGridView.DataSource

list.Add(new pers());

bs = new BindingSource();
bs.DataSource = perlist;

dataGridView1.DataSource = bs;

and the new item will be display

Jordanson answered 31/1, 2017 at 11:58 Comment(3)
I am not sure if I am missing something, but for me changes are reflected both ways even without BindingSourcePresage
@Presage you are right. I wonder what mistake I made last time when I tried and it did not work out. So I saved this in my brain as advantage, because I used BindingSource instead of normal binding and solved my problem.Jordanson
a lot of tutorials and code samples use it, so to most it might seems like it is needed. Visual Studio creates BindingSource when data binding trough the Designer, so there might be some use of it that I am unaware of (maybe separating the filtering and sorting logic from the data source). BindingSource is needed when data binding some collections like Dictionary, but I don's see much use of it otherwise.Presage

© 2022 - 2024 — McMap. All rights reserved.