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.
TextBox txtProductName=new TextBox();
Label lblRecordNumber=new Label(‘);
…………………………………………………………….
SqlDataAdapter sqldaProducts=new SqlDataAdapter(“SELECT * FROM Products”,sqlconProducts);
DataSet dsetProducts=new DataSet(“Products”);
sqldaProducts.Fill(dsetProducts,”Products”);
Binding bndProductName=new Binding(“Text”,dsetProducts,”Products.ProductName”);
txtProductName.DataBindings.Add(bndProductName);
MessageBox.Show(“ProductName field is bound to “ +bndProductName.Control.Name +”text box.”,”Message”);
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.
TextBox txtorderdate=new TextBox();
Binding bndOrderDate;
………………………………….
SqlDataAdapter sqldaOrders=new SqlDataAdapter(“SELECT * from Orders”,sqlconOrders);
DataSet dsetOrders=new DataSet();
sqldaOrders.Fill(dsetOrders,”Orders”);
bndOrderDate=new Binding(“Text”,dsetOrders, “Orders.OrderDate”);
bndOrderDate.Format+=new ConvertEventHandler(bndOrderDate_Format);
txtOrderDate.DataBindings.Add(bndOrderDate);
…………………………
Private void bndOrderDate_Format(object sender, ConvertEventArgs e)
{
e.Value=Convery.ToDateTime(e.Value).ToString(“MM/dd/YYY”);
}
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.
BindingSource bndsrcCustomers;
…………………….
SqlDataAdapter sqldaCustomers=new SqlDataAdapter(“SELECT * from Customers”, sqlconCustomers);
DataSet destCustomers=new DataSet();
sqldaCustomers.Fill(dsetCustomers,”Customers”);
bndsrcCustomers=new BindingSource(dsetCustomers, “Cutomers”);
bndsrcCustomers.AllowNew=true;
bndsrcCustomers.DataSource=dsetCustomers.Tables(“Customers”);
bndsrcCustomers.Filter=”City=’Noida’ ”;
Button btnShowAll=new Button();
DataFridView dgvwCustomers=new DataGridView();
dgvwCustomers.Size=new Size(600,300);
dgvwCustomers.DataSource=bndsrcCustomers;
Controls.Add(dgvwCustomers);
………………..
Private void btnShowAll_Click(object sender , EventArgs e)
{
bndsrcCustomers.RemoveFilter();
}
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.
|
BindingSource bndsrcCustomer;
…………………………………
SqlDataAdapter sqldaCustomers=new SqlDataAdapter(“SELECT * FROM Customers”,sqlconCustomers);
DataSet dsetCustomers=new DataSet();
saldaCustomers.Fill(dsetCustomers,”Customers”);
bndsrcCustomers=new BindingSource(dsetCustomers, “Customers”);
bndsrcCustomers.AllowNew=true;
bndsrcCustomers.DataSource=dsetCustomers.Tables(“Customers”);
txtCustomerID.DataBindings.Add(“Text”, bndsrcCustomer, “CustomerID”);
txtContactName.DataBindings.Add(“Text”, bndsrcCustomer, “ContactName”);
txtCity.DataBindings.Add(“Text”, bndsrcCustomer,”City”);
bndsrcCustomer.PosititonChanged+=new EventHanlder(bndsrcCustomer_PositionChnaged);
bndsrcCustomer.MoveLast();
……………………………..
private void bndsrcCustomer_PositionChanged(object sender,EventArgs e)
{
txtPosition.Text =( bndsrcCustomer.Position+1).ToString();
}
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.
SqlDataAdapter sqldaSuppliers=new SqlDataAdapter(“SELECT * from Suppliers”, sqlconSuppliers);
DataSet dsetSuppliers=new DataSet();
SqldaSuppliers.Fill(dsetSuppliers, “Suppliers”);
BindingSource bndsrcSuppliers=new BindingSource(destSuppliers, “Suppliers”);
bndsrcSuppliers.AllowNew=true;
bndsrcSuppliers.DataSource=dsetSuppliers.Tables(“Suppliers”);
bndsrcSuppliers.Sort=”City DESC”;
DataGridView dgvwSuppliers=new DataGridView();
dgvwSuppliers.Size=new Size(450,110);
dgvwSuppliers.DataSource=bndsrcSuppliers;
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.