Navigating between Records
For every data source that is bound to a Windows Form control, there exists a CurrencyManager object. The CurrencyManager object handles the binding to the data source by keeping a pointer to the current item in the record list.
The CurrencyManager class is derived from the BindingManagerBase class. If all the Windows Form controls are bound to a single data source, the form will have one CurrencyManager object associated with it.
A BindingContext object, which is a Windows Form object, is used to keep track of the existing CurrencyManager objects in a form.
A Windows Form containing bound controls has at least one BindingContext object that keeps track of one or more CurrencyManager objects, each in turn keeping track of a data source. The figure given below illustrates the relationship between the BindingContext class, CurrencyManager class, and a Windows Form:
Fig:- 1.1. Relationship between the BindingContext class, CurrencyManager class and a Windows Form.
Design a Windows Form to display Data.
To design a Windows Form for displaying Data, retrieved from a SQL Server database, perform the following steps:
Create a new Visual Basic Windows Application project by clicking the New project button in Microsoft Development Environment. Form1 is added to the project by default. Name the form as FrmView.
-Select View from the menu bar, and then select ToolBox from the View menu. The Toolbox is displayed.
-From the Windows Forms tab of the Toolbox, drag five label controls and five Textbox controls, to the form.
-Set the Text property of the label controls as displayed in the following table:
Drag four Button controls to the form. Set the Name and Text property for each button as displayed in the following table:
Connect to Database
Connecting to the NorthWind SQL Server database involves creating a connection, a data adapter, and a dataset. To connect to the SQL Server database, perform the following steps:
Create a DataAdapter named SqlDataAdapter1 through the wizard.
- On the second screen of the wizard, click the New Connection button and establish a connection with NorthWind SQL Server database.
- Add the table Customers and select all its fields for the Select command in Query Builder.
- Generate a new Dataset DSCust1.
For steps on creating a connection, a data adapter, and a dataset using a wizard Click Here
Make the following declaration at the Form level:
Dim bm as BindingManagerBase
In this example, a BindingManagerBase class object bm is declared. The object bm is initialized by using the BindingContext() method of the form. The position property of BindingManagerBase class is set to 0, which displays the first row of the DataTable Customer. You can change the value of the position property to specify the row of the DataTable to be bound to the controls.
You will use the BindingManagerBase class and the BindingContext() method to navigate through the records.
Code for the Load event of the form:
Dim bm As BindingManagerBase
Private Sub Frmview_Load(ByVal sender As Object,
ByVal e As System.EventArgs) Handles MyBase.Load
bm = Me.BindingContext(DsCust1, "Customers")
bm.Position = 0
The statement SqlDataAdapter1.Fill(DsCust1) populates the dataset with the records from the Customers table.
Write the following code for navigating through the data.
Private Sub bnnext_Click(ByVal sender As Object,
ByVal e As System.EventArgs) Handles bnnext.Click
bm.Position += 1
Private Sub bnlast_Click(ByVal sender As Object,
ByVal e As System.EventArgs) Handles bnlast.Click
bm.Position = bm.Count - 1
Private Sub bnprev_Click(ByVal sender As Object,
ByVal e As System.EventArgs) Handles bnprev.Click
bm.Position -= 1
Private Sub bnfirst_Click(ByVal sender As Object,
ByVal e As System.EventArgs) Handles bnfirst.Click
bm.Position = 0
Bind the data to a Windows Form Control
Make the Text property under the Appearance category blank for all the TextBox controls. Expand the DataBinding property under the Data category. When the Text property is clicked, a down arrow is displayed. Clicking on the down arrow displays a list of datasets and the corresponding tables. The tables contain the existing column names.
The column names to be selected from the table Customers for the text property of each of the TextBox controls.
To view the bound data, select Debug from the menu bar. Then, select Start from the Debug menu. You can also run the application by clicking the Start icon on the standard toolbar.
Records from the Customers table are displayed. You can navigate through the records by using the First, next, and Previous buttons.