ARTICLE

Working with Data-bounds controls

Posted by Mahesh Chand Articles | ADO.NET in VB.NET May 16, 2004
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.
 
Reader Level:

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) Handles MyBase.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) Handles MyBase.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) Handles MyBase.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.

share this article :
post comment
 

Hi,
I am not getting error in my end, this is working properly. Check the dataset have records or not, before binding the combobox.

Posted by Manish Dwivedi Apr 08, 2009

Thank you very much for all the info here. Even when I follow the same steps, i still get the following error. Could you please let me know the reason. System.Data.DataViewManagerListItemTypeDescriptor Thanks, Shreya

Posted by shreya dr Aug 13, 2008

 

this example is for simplecombo...give the example datacombo in vb.net...it is another version of combobox ...means ms datacombo

datacombo1.rowsource=what

datacombo1.boundcolumn=what

datacombo1.listfield=what

 

 

Posted by siny antony Mar 18, 2006
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Diagram
Become a Sponsor