Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to bind the DataSet object to Datagrid?

How-to

How to bind the DataSet object to Datagrid?

by  PankajBanga  Posted    (Edited  )

This sample refers to the System.Data.OleDb Class Library namespaces and NWIND.mdb (access database).

Imports System.Data.OleDb

Dim strConn As String
Dim strSQL As String
Dim Conn As OleDbConnection

Dim objDA As OleDbDataAdapter
Dim objDS As New DataSet()

Try
'MAKE SURE TO CHANGE THE PATH OF NWIND.mdb AS PER ON YOUR MACHINE.
'Build the Connection strings.
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Program Files\Microsoft Visual Studio\VB98\NWIND.mdb"

'Pass the Connection string to OleDbConnection object.
Conn = New OleDbConnection(strConn)

'Build the SQL strings.
strSQL = "SELECT EmployeeID, FirstName, LastName " & _
"FROM Employees "

'Initialize the OleDbDataAdapter with SQL and Connection string,
'and then use the OleDbAdapter to fill the DataSet with data.
objDA = New OleDbDataAdapter(strSQL, Conn)
objDA.Fill(objDS, "Employees")

'Bind the DataSet to DataGrid.
DataGrid1.SetDataBinding(objDS, "Employees")

Catch Excep As System.Exception
MessageBox.Show(Excep.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top