I think I understand well enough what the BindingSource class does - i.e. provide a layer of indirection between a data source and a UI control. It implements the IBindingList i
Pretty old question. Wonder why anyone hasn't answered it till now. OK, I'll try and share things from my experience.
A BindingSource
is more than just a way to bind controls to collections. After having worked in WinForms for over a decade, the best features of a BindingSource
that I like the most include:
BindingSource
can act as a data source of another BindingSource
.To fully appreciate these features, I'll explain them in the context of a DataSet, which is by far the most common type of data source used in WinForms, especially in line-of-business apps.
Currency management boils down the concept of current record. A DataTable
is just a collection of DataRow
s, i.e. there is no concept of current record in DataTables. Same is the case of DataView
(on a side note, you cannot directly bind to a DataTable
; when you do that, it actually binds to the DefaultView
property of that DataTable
, which is a DataView
. You could create your own DataView
too).
Currency management really proves handy in case of Master/Detail kind of UI. So let's say you have a ListBox
of Students in the left pane (Master), and several TextBoxes, ComboBoxes, CheckBoxes etc. in the right pane, with a grid of selected student's courses (Detail). In your DataSet, you have two DataTables named Student
and Courses
. For the sake of simplicity, I'm avoiding a gerund (Student_Course) here. The Course
table has a foreign key StudentID
. Here's how you setup binding here (note how all the 3 features I listed above are used in the setup below):
BindingSource
controls to your form, named bsStudent
and bsCourses
.DataSource
of bsStudent
to Student
DataTable.DataSource
of bsCourses
to bsStudent!DataMember
property, you'll see the name of the relation that exists in the DataSet between our two tables. Select it!bsStudent
's properties.DataSource
of courses grid bsCourses.And you're done. Without writing a single line of code (so to speak), you have successfully created a master-details view. The BindingSource control will now take care of the current record in Students list and update not only the atomic controls (TextBoxes, ComboBoxes etc.), but also the courses grid, which will automatically update its contents to show the courses of currently selected student.
This, my friend, is the role of BindingSource (among other nice things like sorting, filtering etc.) which I like the most. Without involving a BindingSource
in-between your controls and the data store, you'd not have the concept of current record and therefore would manually have to manage keeping all the UI in sync.
I also would like to add, using BindingSource, you can bind to a business object, which can implement INotifyPropertyChanged event, so if data is changed (whether by your code or someone else's code), your UI can automatically reflect the updates.
Hi I also have some confusion about the subject.
When I use datatables those implement all the interfaces.
However I always use the bindingsource just to be sure.. :)
There are some arguments why that I can think of