Search This Blog

Thursday, June 30, 2011

BindingSource object in .net


If you like this article, please click on +1 button and share with your friends.

Click here for the Video Tutorial of this article.
visit www.iGnani.com


What is a BindingSource?

The BindingSource component provides a layer of indirection between the underlying data source and the bound UI controls.

Note: In computer programming, indirection is the ability to reference something using a name, reference, or container instead of the value itself.

Instead of binding the controls directly to the data sources or collections of data objects, you instead bind the controls to a BindingSource object. By being a middle tier object between the data source and controls, the BindingSource can provide services on behalf of the data source. In Simple words, it is a broker object between your databound controls and the datasource.

It was first introduced in .net 2.0 and is supported in all the versions till the latest being .net 4.0. It is made available through the namespace System.Windows.Forms.

Why do I need it?

BindingSource serves many purposes.

BindingSource object helps us by simplifying process of binding controls on a form to data by providing currency management, change notification, and other services between Windows Forms controls and data sources. It accomplishes this by attaching the BindingSource object to your data source using the DataSource property. In case of complex binding scenarios, set the DataMember property to a specific column or list in the data source, though this is optional. Then bind controls to the BindingSource. All further interaction with the data is accomplished with calls to the BindingSource component.

In addition to the above, it can act as a strongly typed data source. The type of the underlying data source is fixed through one of the following ways:

  • Using the Add method to add an item to the BindingSource component.

  • Set the DataSource property to a list, single object, or type.

    Both of these mechanisms create a strongly-typed list. You can also use the BindingSource to bind your controls to a factory object.

    BindingSource object provides methods for accessing the underlying data. The current item can be retrieved through the Current property, and the entire list can be retrieved using the List property. Editing operations are supported on the current item through Add, AddNew, EndEdit, CancelEdit, Current and the RemoveCurrent methods. Although currency management is handled automatically for all underlying data source types, this class exposes a number of events, such as DataSourceChanged (Occurs when the DataSource property value has changed), CurrentItemChanged (Occurs when a property value of the Current property has changed), ListChanged (Occurs when the underlying list changes or an item in the list changes) to name a few.

    Data sources that are bound to a BindingSource component can also be navigated and managed with the BindingNavigator class, which provides a VCR-like user interface for navigating items within a list. Although BindingNavigator can be bound to any data source, it was designed to integrate with a BindingSource component through its BindingNavigator.BindingSource property.

    The default property for the BindingSource class is DataSource. The default event is CurrentChanged.

    BindingSource can be bound to any of the following:

    • Object
    • System.Type
    • IEnumerable
    • ICollection
    • IList
    • IListSource
    • IBindingList
    • IBindingListView

    One simple usage is in developing a MasterDetail form by using a BindingSource to connect to multiple DataGridView tables to a single datasource, there by we can bind a parent table to one grid and display the related records in child table in a second grid.

    BindingSource _bindingSource = new BindingSource(); 
    conn.Open(); 
    SqlDataReader reader = cmd.ExecuteReader(); 
    _bindingSource.DataSource = reader; 
    conn.Close(); 
    DataGridView.DataSource = _bindingSource; 

    The above code shows a simple way to bind a DataGridView object to a BindingSource object.





    visit www.iGnani.com

    MicroMind Information Systems
    #4013, K.R. Road, Banashankari II Stage,
    BANGALORE - 560070
    KARNATAKA, INDIA
    Phone: +91 80 26762747




    Technorati Tags: , , , , , , , , , , , , , , ,


  • No comments:

    Post a Comment