Binding Data to
controls:
In ADO.NET
perspective, for binding data to various Controls such as List Control,
DataGridView Control and other Data controls. Data Binding is necessary to link
the Web or Window form's UI controls to the data that is retrieved from the data
Source.
Simple Data
Binding:
When you only need to
have a control display a single value, like that of a TextBox or a RichTextBox
Controls, this limited usages is referred to as "Simple Data Binding". Means to
that Simple Data Binding is the ability to bind a control to a single Data
element (Such as a value in a Column in a DataSet Table).
Complex Data
Binding:
When you need to
display and manipulate more than one data element at a time , like that
ListBoxes, ComboBoxes, DAtaGridview and CheckedListBox. This extended usage is
referred to as "complex Data Binding".
Creating Simple
and Complex Binding in window Forms:
-
Create a new project in VB.NET, Choosing Window Forms.
-
Design Window forms with Simple and complex controls.


-
Create Data Base in Ms Access.

-
Coding for Module1:
Imports System.Data.OleDb
Module Module1
Public constringAs OleDbConnection =New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=D:\_db3.accdb")
End Module
-
Coding for first form :
Imports System.Data.OleDb
Public Class Form1
Dim Da As OleDbDataAdapter
Dim SqlString As String
Private Sub Label4_Click(ByVal senderAs System.Object, ByVal e As System.EventArgs)Handles Label4.Click
End Sub
Private Sub Button3_Click(ByVal senderAs System.Object, ByVal e As System.EventArgs)Handles Button3.Click
Me.Close()
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal senderAs System.Object, ByVal e As System.EventArgs)Handles ComboBox1.SelectedIndexChanged
End Sub
Private Sub Button2_Click(ByVal senderAs System.Object, ByVal e As System.EventArgs)Handles Button2.Click
Dim Control_BindingAs New Control_Binding
Control_Binding.Show()
Me.Visible =False
End Sub
Private Sub Button1_Click(ByVal senderAs System.Object, ByVal e As System.EventArgs)Handles Button1.Click
Try
constring.Open()
SqlString = "insert into info2 values("
SqlString += """" & TextBox1.Text & """"
SqlString += ","
SqlString += """" & TextBox2.Text & """"
SqlString += ","
SqlString += """" & ComboBox1.SelectedItem & """"
SqlString += ","
SqlString += """" & CheckedListBox1.SelectedItem & """"
SqlString += ")"
Dim DaAs New OleDbDataAdapter()
Da.InsertCommand = New OleDbCommand(SqlString, constring)
Da.InsertCommand.ExecuteNonQuery()
MessageBox.Show("A Record inserted through DataAdopter ")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class
-
Coding for second Form:
Imports System.Data.OleDb
Public Class Control_Binding
Dim Da As OleDbDataAdapter
Dim SqlString As String
Dim Ds As DataSet
Dim CurrentRow As Integer
Private Sub Button3_Click(ByVal senderAs System.Object, ByVal e As System.EventArgs)Handles Button3.Click
Me.Close()
End Sub
Private Sub Button4_Click(ByVal senderAs System.Object, ByVal e As System.EventArgs)Handles Button4.Click
Dim Form1 As New Form1
Form1.Show()
Me.Close()
End Sub
Private Sub Control_Binding_Load(ByVal senderAs System.Object, ByVal e As System.EventArgs)Handles MyBase.Load
Try
Dim sAs String
CurrentRow = 0
s = TextBox1.Text
constring.Open()
Ds = New DataSet
SqlString = "select * from info2 order by EmployeeId"
Da = New OleDbDataAdapter(SqlString, constring)
Da.Fill(Ds, "info2")
TextBox1.Text = Ds.Tables("info2").Rows(CurrentRow)("EmployeeId")
TextBox2.Text = Ds.Tables("info2").Rows(CurrentRow)("EmployeeName")
ComboBox1.Text = Ds.Tables("info2").Rows(CurrentRow)("EmployeeCompany")
TextBox3.Text = Ds.Tables("info2").Rows(CurrentRow)("EmployeeDesignation")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button1_Click(ByVal senderAs System.Object, ByVal e As System.EventArgs)Handles Button1.Click
CurrentRow += 1
TextBox1.Text = Ds.Tables("info2").Rows(CurrentRow)("EmployeeId")
TextBox2.Text = Ds.Tables("info2").Rows(CurrentRow)("EmployeeName")
ComboBox1.Text = Ds.Tables("info2").Rows(CurrentRow)("EmployeeCompany")
TextBox3.Text = Ds.Tables("info2").Rows(CurrentRow)("EmployeeDesignation")
End Sub
Private Sub Button2_Click(ByVal senderAs System.Object, ByVal e As System.EventArgs)Handles Button2.Click
CurrentRow -= 1
TextBox1.Text = Ds.Tables("info2").Rows(CurrentRow)("EmployeeId")
TextBox2.Text = Ds.Tables("info2").Rows(CurrentRow)("EmployeeName")
ComboBox1.Text = Ds.Tables("info2").Rows(CurrentRow)("EmployeeCompany")
TextBox3.Text = Ds.Tables("info2").Rows(CurrentRow)("EmployeeDesignation")
End Sub
End Class
Output:


