The Role Of Binding Class In .NET

Introduction

 
The Binding class is used to bind a property of a control with the property of an object. For creating a Binding object, the developer must specify the property of the control, the data source, and the table field to which the given property will be bound.
 

Properties and Events of Binding class

 
BindingMemberInfo
 
This property retrieves an object that contain the current binding info, based on the dataMember parameter in the Binding constructor.
 
ControlUpdateMode
 
This property specifies or retrieves when data source changes are transferred to the bound control property.
 
IsBinding
 
This property retrieves a value that indicates whether the binding is active.
 
Format
 
This event occurs when the data value is bound to the property of a control.
 
The following source code demonstrate how to use the control property of the Binding class.
  1. TextBox txtProductName=new TextBox();  
  2. Label lblRecordNumber=new Label(‘);  
  3. …………………………………………………………….  
  4. SqlDataAdapter sqldaProducts=new SqlDataAdapter(“SELECT * FROM Products”,sqlconProducts);  
  5. DataSet dsetProducts=new DataSet(“Products”);  
  6. sqldaProducts.Fill(dsetProducts,”Products”);  
  7. Binding bndProductName=new Binding(“Text”,dsetProducts,”Products.ProductName”);  
  8. txtProductName.DataBindings.Add(bndProductName);  
  9. MessageBox.Show(“ProductName field is bound to “ +bndProductName.Control.Name +”text box.”,”Message”);  
  10. lblRecordNumber.Text=Convert.ToString(bndProductName.BindingManagerBasePosition + 1);  
In this source code, a TextBox and a Label control is created. This records from the Products table are fetched using the SqlDataAdapter and DataSet. An instance of the Binding class is created with three parameters. The ProductName column of the Products table is bound to the textbox. The message box is displayed stating that the product field is bound to txtProductName control. The label lblRecordNumber , displays the position of the record that is displayed using the BindingManagerBase property of the Binding object.
 
The source code demonstrate how to use the Format event of the Binding class.
  1. TextBox  txtorderdate=new  TextBox();  
  2. Binding bndOrderDate;  
  3. ………………………………….  
  4. SqlDataAdapter sqldaOrders=new SqlDataAdapter(“SELECT * from Orders”,sqlconOrders);  
  5. DataSet dsetOrders=new DataSet();  
  6. sqldaOrders.Fill(dsetOrders,”Orders”);  
  7. bndOrderDate=new Binding(“Text”,dsetOrders, “Orders.OrderDate”);  
  8. bndOrderDate.Format+=new ConvertEventHandler(bndOrderDate_Format);  
  9. txtOrderDate.DataBindings.Add(bndOrderDate);  
  10. …………………………  
  11. Private void bndOrderDate_Format(object sender, ConvertEventArgs e)  
  12. {  
  13.    e.Value=Convery.ToDateTime(e.Value).ToString(“MM/dd/YYY”);  
  14. }  
In this source code, a Text Box control namely, txtOrderDate is created. The records from the Orders table are fetched using the SqlDataAdapter and DataSet . An instance of the Binding class is created with three parameters. The DataBindings property binds the Binding object to textbox. This binds the OrderDate column of the Orders table to textbox. The Format event is raised before the TextBox control displays the date value. This event formats the data in the MM/DD/YYYY format.
 

BindingSource Class

 
The BindingSource class is used to encapsulate a data source in a form. The properties of this class can be used to get the list to which the connector is bound.
 

Properties of BindingSource Class

 
AllowNew
 
Specifies or retrieves a value which indicates that the AddNew() method can be used to add items to the list.
 
Filter
 
Specifies or retrieves the expression used to filter rows which are viewed.
 
List
 
Retrieves the list to which the connector is bound.
 
RaiseListChangedEvents
 
Specifies or retrieves a value that indicates whether the ListChanged events should be raised.
 
Sort
 
Specifies or retrieves the name of columns used for sorting and sort order to view rows in the data source.
 

Methods of BindingSource Class

 
Find
 
Finds a particular item in the data source.
 
GetListName
 
Retrieves the name of list that supplies data for binding.
 
GetRelatedCurrencyManager
 
Retrieves the related currency manager for a particular data member.
 
IndexOf
 
Searches for an object and returns the index of its first occurrence in the list.
 
RemoveFilter
 
Removes the filter which is associated with the BindingSource.
 
ResetCurrentItem
 
Enables the control bound to BindingSource to re-read the currently selected item. It also refreshes the display value.
 
ResetItem
 
Enables the control bound to BindingSource to reread the item at the specified index, it also retrieves the display value.
 
The following source code creates a DataGridView control and displays the filtered records from the database.
  1. BindingSource bndsrcCustomers;  
  2. …………………….  
  3. SqlDataAdapter sqldaCustomers=new SqlDataAdapter(“SELECT * from Customers”, sqlconCustomers);  
  4. DataSet destCustomers=new DataSet();  
  5. sqldaCustomers.Fill(dsetCustomers,”Customers”);  
  6. bndsrcCustomers=new BindingSource(dsetCustomers, “Cutomers”);  
  7. bndsrcCustomers.AllowNew=true;  
  8. bndsrcCustomers.DataSource=dsetCustomers.Tables(“Customers”);  
  9. bndsrcCustomers.Filter=”City=’Noida’ ”;  
  10. Button btnShowAll=new Button();  
  11. DataFridView dgvwCustomers=new DataGridView();  
  12. dgvwCustomers.Size=new Size(600,300);  
  13. dgvwCustomers.DataSource=bndsrcCustomers;  
  14. Controls.Add(dgvwCustomers);  
  15. ………………..  
  16. Private void btnShowAll_Click(object sender , EventArgs e)  
  17. {  
  18.    bndsrcCustomers.RemoveFilter();  
  19. }  
In this source code , records from the Customers table are fetched using the SqlDataAdapter and DataSet. The BindingSource object is created to refer to the DataSet bound to the table. The AllowNew property is set to true, which invokes the AddNew() method to add items to the list. The DataSource property of the BindingSource object is set to the DataSet object. The Filter property retrieves the records, which contain the value Noida in the city column. The DataGridView Control is created and the DataSource property of this control is set to the BindingSource object. This displays the filtered records of the BindingSource object in a grid view. When the user clicks the ShowAll button, the RemoveFilter() method displays all the records from the table by removing the filter applied on them.
 

Events of BindingSource class

 
Event
Description
AddingNew
Occurs before an item is inserted to the list.
CurrentItemChanged
Occurs when the value of the Current property has been changed.
ListChanged
Occurs when the item or list in the list changes.
PositionChanged
Occurs when the value of the Position property has been changed.
  1. BindingSource bndsrcCustomer;  
  2. …………………………………  
  3. SqlDataAdapter sqldaCustomers=new SqlDataAdapter(“SELECT * FROM Customers”,sqlconCustomers);  
  4. DataSet dsetCustomers=new DataSet();  
  5. saldaCustomers.Fill(dsetCustomers,”Customers”);  
  6. bndsrcCustomers=new BindingSource(dsetCustomers, “Customers”);  
  7. bndsrcCustomers.AllowNew=true;  
  8. bndsrcCustomers.DataSource=dsetCustomers.Tables(“Customers”);  
  9. txtCustomerID.DataBindings.Add(“Text”, bndsrcCustomer, “CustomerID”);  
  10. txtContactName.DataBindings.Add(“Text”, bndsrcCustomer, “ContactName”);  
  11. txtCity.DataBindings.Add(“Text”, bndsrcCustomer,”City”);  
  12. bndsrcCustomer.PosititonChanged+=new EventHanlder(bndsrcCustomer_PositionChnaged);  
  13. bndsrcCustomer.MoveLast();  
  14. ……………………………..  
  15. private void bndsrcCustomer_PositionChanged(object sender,EventArgs e)  
  16. {  
  17.    txtPosition.Text =( bndsrcCustomer.Position+1).ToString();  
  18. }  
This source records from Customers table are fetched using the SqlDataAdapter and DataSet. An object of the BindingSource class is created to refer to the DataSet bound to the table. The DataSource property of the BindingSource object is set to the DataSet object. The DataBindings property binds the BindingSource object to txtCustomerID, txtContactName, and txtCity to display the ID, name and city respectively. The PositionChanged event occurs when the index position of the current record is changed . This event displays the position of the record in the TextBox control, txtPostiton.
 

Sort Property of the BindingSource Class

 
The Sort property of the BindingSource class is used to sort rows in a table. The internal list supports sorting only if it implements the IBindingList or IBindingListView interfaces. The list will change depending on the interfaces being implemented.
 

Properties of IBindingList and IBindingListView Interfaces.

 
SortProperty
 
When the BindingSource object implements the IBindingList interface, this property is set. The property retrieves the PropertyDescriptor used for sorting the list. The PropertyDescriptor class provides information about the property used for sorting such as name, attributes, and the type.
 
SortDirection
 
When the BindingSource object implements the IBindingList interface, this property is set. This property retrieves the sorting order such as ascending or descending order. These values are defined in the ListSortDirection enumeration.
 
SortDescriptions
 
When the BindingSource object implements the IBindingListView interface, this property is set.
 
Source Code below uses the Sort property to sort the records of the table.
  1. SqlDataAdapter  sqldaSuppliers=new SqlDataAdapter(“SELECT * from Suppliers”, sqlconSuppliers);  
  2. DataSet dsetSuppliers=new DataSet();  
  3. SqldaSuppliers.Fill(dsetSuppliers, “Suppliers”);  
  4. BindingSource bndsrcSuppliers=new BindingSource(destSuppliers, “Suppliers”);  
  5. bndsrcSuppliers.AllowNew=true;  
  6. bndsrcSuppliers.DataSource=dsetSuppliers.Tables(“Suppliers”);  
  7. bndsrcSuppliers.Sort=”City DESC”;  
  8. DataGridView dgvwSuppliers=new DataGridView();  
  9. dgvwSuppliers.Size=new Size(450,110);  
  10. dgvwSuppliers.DataSource=bndsrcSuppliers;  
  11. Controls.Add(dgvwSuppliers);  
This source code , records from the suppliers table are fetched using the SqlDataAdapter and DataSet. An object of the BindingSource class is created to refer to the DataSet bound to the table. The DataSource property of the BindingSource object is set to the DataSet object. The Sort property sorts the City column of the Suppliers table in descending order. The DataGridView control is created and the DataSource property of this control is set to the BindingSource object. This displays the records of the BindingSource object in a grid view.
 

Summary

 
The Binding class is used to bind a property of a control with the property of an object. BindingMemberInfo property retrieves an object that contains the current binding information, which is based on the dataMember parameter in the Binding constructor. The Sort property of the BindingSource class is used to sort rows in a table.