Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BindingSource - what are the advantages of using BindingSource

Tags:

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;
like image 377
Adam Filipek Avatar asked Jan 31 '17 11:01

Adam Filipek


People also ask

What is BindingSource?

The BindingSource component acts as an intermediary that provides binding and currency management services. At design time or run time, you can bind a BindingSource component to a complex data source by setting its DataSource and DataMember properties to the database and table, respectively.

What is BindingNavigator?

The BindingNavigator control is composed of a ToolStrip with a series of ToolStripItem objects for most of the common data-related actions: adding data, deleting data, and navigating through data. By default, the BindingNavigator control contains these standard buttons.

What is data binding in VB net?

Description. Simple data binding. The ability of a control to bind to a single data element, such as a value in a column in a dataset table. Simple data binding is the type of binding typical for controls such as a TextBox control or Label control, which are controls that typically only display a single value.


2 Answers

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

like image 50
kgzdev Avatar answered Oct 05 '22 04:10

kgzdev


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

like image 36
Mong Zhu Avatar answered Oct 05 '22 05:10

Mong Zhu