Data-bounds controls in VB.NET

Working with data-bound controls in VS.NET IDE is a fun. You just set couple of properties and you're all set to view, add, and update data using these data-bound controls. Some of these common controls are DataGrid, DataCombo, and ListBox.
  • 4648

Working with data-bound controls in VS.NET IDE is a fun. You just set couple of properties and you're all set to view, add, and update data using these data-bound controls. Some of these common controls are DataGrid, DataCombo, and ListBox.

In this article, I'll show you how to connect your data source to these controls using ADO.NET components. In my sample applications, I've used SQL Server database. Working with OLE-DB data sources or ODBC data sources are same accept the database connection.

You need to add these namespaces before using ADO.NET components.

Imports System
Imports System.Data
Imports System.Data.SqlClient

DataGrid Control

Add a datagrid control to a form and write the following code. Change your data source, table and column names if you're using different databases.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
Dim cmdString As String = "Select * from Employees"
Dim connString As String = "user id=sa; password=; database=northwind; server=MCB"
Dim myConnection As SqlConnection = New SqlConnection(connString)
Try
' Open connection
myConnection.Open()
Dim da As SqlDataAdapter = New SqlDataAdapter(cmdString, myConnection)
Dim dataSet1 As DataSet = New DataSet
da.Fill(dataSet1, "Employees")
DataGrid1.DataSource = dataSet1.DefaultViewManager
Catch ae As SqlException
MessageBox.Show(ae.Message)
End Try
End
 Sub
 

ListBox Control

Add a list box control to a form and add the following code to the form load event.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
Dim cmdString As String = "Select * from Employees"
Dim connString As String = "user id=sa; password=; database=northwind; server=MCB"
Dim myConnection As SqlConnection = New SqlConnection(connString)
Try
' Open connection
myConnection.Open()
Dim da As SqlDataAdapter = New SqlDataAdapter(cmdString, myConnection)
Dim dataSet1 As DataSet = New DataSet
da.Fill(dataSet1, "Employees")
ListBox1.DataSource = dataSet1.DefaultViewManager
ListBox1.DisplayMember = "LastName"
Catch ae As SqlException
MessageBox.Show(ae.Message)
End Try
End
 Sub
 

Combo Box

Just add a combo box to a form and add the following code:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
Dim cmdString As String = "Select * from Employees"
Dim connString As String = "user id=sa; password=; database=northwind; server=MCB"
Dim myConnection As SqlConnection = New SqlConnection(connString)
Try
' Open connection
myConnection.Open()
Dim da As SqlDataAdapter = New SqlDataAdapter(cmdString, myConnection)
Dim dataSet1 As DataSet = New DataSet
da.Fill(dataSet1, "Employees")
Dim DataViewManager1 As DataViewManager = dataSet1.DefaultViewManager
ComboBox1.DataSource = DataViewManager1
ComboBox1.DisplayMember = "Employees.FirstName"
Catch ae As SqlException
MessageBox.Show(ae.Message)
End Try
End
 Sub

umm ... I think that's it.
Cheers.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.