How to use Data Binding in VB.Net
Data Binding in VB.Net which explains everything to bind controls of VB.net to a Data source.
Binding Context, Currency Manager and Binding Controls are also explained in this article.
It also shows how to Move from one record to another as well as to first and the last record in the Dataset object
Data binding: -
It is a way for programmers to create a link between the controls on a form and the data source.
The Data Grid view control is used to display all the records at a single time. The programmers can use it to Insert, Update and delete the records. However, sometimes we want to bind a single filed from a table to a simple control such as Text Box on a form. This type of binding also gives programmers more controls over data but also increases complexity of the program because the programmer has to write code to bind the data to the controls on the form and also write the code to navigate between the records.
Binding Context: - Each VB.Net form has a built in Binding Context object that manages the bindings of the VB.Net form controls. It is already built into each VB.Net form and needs not to be setup.
Currency Manager: - It manages the current position of the binding to the Data Source. The current even fires when there is a change in it.
It is the Currency Manager which is responsible for keeping the data bound controls in sync with the data source and with other data bound controls that use the same data source.
If there are multiple data sources in a form, we can declare multiple currency manager variable and refer them to appropriate Currency Manager Objects for a given data source.
e.g
Dim objcm as currencymanager
Objcm=Ctype(me.bindingcontext(objdataset),currencymanager)
Here Ctype is a function used to return an object that is explicitly converted. This function accepts two arguments:
1. The expression to be converted.
2. The type to which this expression is to be converted
We can also use the position property of the Currency Manager to manage the position of the records.
e.g
• To move the next record from the current position, we can use the following code
objcm.position +=1
• To move backward one record from the current position, we can use the following code
objcm.position - =1
• To move to the First record, we can use the following code
objcm.position =0
• To move to the Last record, we can use the following code
objcm.position =objcm.count-1
Binding Controls: - If we want to bind a data source to a controlon a form, we set the data binding property of that control as shown below;
textbox1. DataBindings. Add ("Text",objdataview,"username")
Here the add method creates a binding for the control Textbox and adds it to controlbindings collection.