What are the benefits of using a bindingsource with bindinglist<business obj> as datasource?
Asked Answered
P

3

15

I can directly bind my DataGridView control to a bindinglist of my business objects by setting the DataSource property. My business object implements INotifyPropertyChanged, so the DGV gets updated when a new item is added to the Binding List or existing one is updated.

With respect to dealing with single records, I can bind my business object to textboxes and other relevant controls.

I can also derive from BindingList and create a CustomBindingList class to implement required methods of IBindable, as explained in the link below : http://msdn.microsoft.com/en-us/library/aa480736.aspx

Alternatively, I have seen people recommend using a BindingSource. BindingSource's Datasource is the business object and the DGV's DataSource is the BindingSource.

In any case, basing it on a BindingSource does not offer me:

  1. Filtering (Filter does not work). Implementation needs to be provided by me.
  2. Sort and Search does not work. Implementation needs to be provided by me.

So, Why is the BindingSource approach recommended?

Broader Picture: Am new to OOPS concepts and C#. Working with Database applications. Winforms. So far have only used DataSet / DataTable approach. Now trying to create and use my own custom classes.

Typically have a Master/Detail form. When I click on a Detail row in the DGV, I want to edit that record in a separate window. So I need to get a handle on the list item represented by that row in the DGV. Trying to find a solution for that has brought me to this point and this doubt.

Given what I want to do, which approach is better and why?

Some pointers here would really help as I am very new to this.

Planking answered 22/4, 2012 at 9:0 Comment(1)
Possible duplicate of C# DataGridView.DataSource difference between using BindingSource and notHomogenize
C
3

It is recommended to use a BindingSource when multiple controls on the form use the same datasource (Behind the Scenes: Improvements to Windows Forms Data Binding)

Design-time: I personally find the BindingSource very helpfull when choosing the properties from my business object when databinding to controls.

To get a handle to the currently selected row, try bindingSource1.Current as MyBusinessObject;

As for filtering and searching: I use a third party dll for grids that have that implemented. So can't help you with that, sorry.

When you work with lists of different types of business objects, don't use the list directly

List<IAnimal> animals = new List<IAnimal>();
animals.Add(new Cat());
animals.Add(new Dog());
bindingSource1.DataSource = animals;

Instead use a BindingList like this:

bindingSource1.DataSource = new BindingList<IAnimal>(animals);

That will make sure all accessed objects in the list are of type IAnimal and saves you some exceptions.

Certified answered 23/4, 2012 at 22:18 Comment(0)
X
1

Binding to a DataSource could give you benefits when dealing with a large set only a part of which gets displayed. For instance if you look at the Telerik ListView here http://www.telerik.com/help/winforms/listview-databinding.html (there are many of these component packages, this is just the latest one I am using bits and pieces from).

The view is very lightweight and lets your scroll position determine which objects need to be actually displayed. So if you only look at the first 10 objects and never scroll down only 10 get bound and displayed. This potentially avoids a lot of unneeded data access.

Their GridView functions in the same manner. There is the displayed part of the grid which is separate from the potentially huge underlying grid.

As a bonus, you get filtering, sorting, grouping.

Xanthochroism answered 11/5, 2012 at 1:44 Comment(0)
S
-2

As far as I know, if you are working with a database, you use a bindingSource in the middle in order to establish a bilateral bridge between the database and your control. Otherwise you can just use a bindingList as source for your control.

Sewoll answered 15/1, 2014 at 4:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.