When is it worth using a BindingSource?

后端 未结 3 1226
萌比男神i
萌比男神i 2021-02-02 08:20

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

3条回答
  •  广开言路
    2021-02-02 08:43

    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:

    1. Binding (of course!)
    2. Currency management (I'll come to that in a second)
    3. A 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 DataRows, 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):

    1. Add two BindingSource controls to your form, named bsStudent and bsCourses.
    2. Set DataSource of bsStudent to Student DataTable.
    3. Set DataSource of bsCourses to bsStudent!
    4. In the DataMember property, you'll see the name of the relation that exists in the DataSet between our two tables. Select it!
    5. Set the binding of individual atomic controls to bsStudent's properties.
    6. Set the 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.

提交回复
热议问题