I have been working on a Windows application where .NET 3.5 has been used. In this application, we were supposed to subscribe to WebServices which are interacting through business objects. Some of the business objects had already been defined as we were enhancing one existing application. Now I was supposed to find out an approach using which we could use databinding in the UI layer. After browsing the internet, I decided to stick with BindingList and give a little more flexibility to wrap this in a BindingSource.
Ideas which came to my mind:
- Business Objects are going to be used for data communication between layers.
- Need a fast and easy implementation approach.
- Approach should help developers by reducing lines of codes through Databinding.
Details of BindingList and BindingSource
I will explain in this post how easily anyone can use
BindingSource. I have taken a Master-Detail relational example – Employee and Employee Details.
Create two separate business entities of
EmpDetails. Now to create the
BindingList classes for respective business entities, use the following code –
public class EmployeeBEList : BindingList<EmployeeBE>
public class EmpDetailsList : BindingList<EmpDetails>
Now in the
EmployeeBE have the reference of
BindingList<EmpDetails> to maintain the Master-Detail relationship. So this is a very fast and easy implementation. This can be used for the existing business objects as well.
Once Business-Objects are created, let’s focus into the UI part. Create a simple search panel consisting of different search criteria. Also have a couple of grids for Employees and Employee Details for the selected Employee. Now comes the
BindingSource using which we will do the design time data-binding.
As shown in the image, add the
EmployeeBE as the data source to the
BindingSource of the search criteria. In the same manner, we can have
bindinglist for the
datagrids where search result would be displayed. This
bindingsource would have the
EmployeeBEList as datasource. Now bind each of the UI elements with respective
I would like to explain the databinding the of the State dropdown. To populate this State dropdown, you can have
Description properties). Similarly, another
BindingList for this
. Now associate the dropdown to
BindingSource which has the
MasterBEList as the datasource. Set two properties of the dropdown as mentioned below -
DisplayMember – “Description” and
ValueMember – “Code”. These are the properties of the
MasterBE. So far we are done with population and displayof States. Now we have to do the changes so that we can pass the searched State code to the next layer from UI. Using the following code (or using designer), the
SelectedValue property of the dropdown is bound to “
("SelectedValue", this.bindingSourceSearchCriteria, "StateCode",
The important thing to note is the
DataSourceUpdateMode which is set to
OnPropertyChanged. This means if the value of
SelectedValue is changed through code also, that would be reflected in the
OnValidation needs user interaction.
Now I would quickly go through the CRUD operations. While retrieving, get the list of objects returned from the DAL layer and associate to
private void btnSearch_Click(object sender, EventArgs e)
DAL objDal = new DAL();
EmployeeBE searcchCriteria = (EmployeeBE)bindingSourceSearchCriteria.Current;
Now the rest of the things would be taken care of by
BindingSource once the data is populated. To add a new record, i.e.
BusinessObject, use the following code snippet:
private void btnAdd_Click(object sender, EventArgs e)
EmployeeBE newEmp = this.bindingSourceSearchResult.AddNew() as EmployeeBE;
For deletion, you need to simply delete the
BusinessObject from the
private void btnDelete_Click(object sender, EventArgs e)
EmployeeBE selectEmp = this.bindingSourceSearchResult.Current as EmployeeBE;
BindingSource can be saved after doing create/delete/update on the list. This is mainly used for bulk update.
DataSet, you have to maintain a list to keep track of the changed items list. I have done that by keeping an array of integers and trapping the
ListChanged event of
private void bindingSourceSearchResult_ListChanged
(object sender, ListChangedEventArgs e)
if (!_addedRowIndexes.Contains(e.NewIndex)) _addedRowIndexes.Add(e.NewIndex);
if (!_changedRowIndexes.Contains(e.NewIndex) &&
So far this has been a very easy and fast implementation approach with design time databinding. There might be other ways of implementing the same scenario but I felt that this one was pretty easy. Definitely,
BindingList does not support by default
Sorting which I will cover in one of my next posts.
- 20th June, 2009: Initial post
This is the my article on
Databinding. In the next one, I would discuss about few more advanced areas of