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:
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.
The BindingSource component serves many purposes. First, it simplifies binding controls on a form to data by providing currency management, change notification, and other services between Windows Forms controls and data sources.
The BindingSource component acts as both a conduit and a data source for other controls to bind to. It provides an abstraction of your form's data connection while passing through commands to the underlying list of data.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With