0% found this document useful (0 votes)
16 views

Data Grid

This document provides steps to populate a Windows Forms DataGrid control with data from an Access database: 1. Add references to System.Data and System.Data.OleDb namespaces. 2. Create an OleDbDataAdapter, connecting it to a table in an Access database using a connection string. 3. Use the DataAdapter's Fill method to populate a DataSet with the database table's data. 4. Set the DataGrid's DataSource property to the default view manager of the DataSet, binding the grid to display the DataSet's data.

Uploaded by

mikee27tan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Data Grid

This document provides steps to populate a Windows Forms DataGrid control with data from an Access database: 1. Add references to System.Data and System.Data.OleDb namespaces. 2. Create an OleDbDataAdapter, connecting it to a table in an Access database using a connection string. 3. Use the DataAdapter's Fill method to populate a DataSet with the database table's data. 4. Set the DataGrid's DataSource property to the default view manager of the DataSet, binding the grid to display the DataSet's data.

Uploaded by

mikee27tan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Starting: Create a Windows Application type project and add a DataGrid control to the

form. Leave DataGrid name as DataGrid1.

Add Reference to the Namespace

First thing you need to do is add reference to System.Data.OleDb namespace since I will
use OldDb data providers. if System.Data namespace is not present, you might want to add
this too.

Imports System.Data
Imports System.Data.OleDb

Create OleDbDataAdapter Object

Now you create a OleDbDataAdapter object and connect to a database table.

' create a connection string


Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\\Northwind.mdb"
Dim myConnection As OleDbConnection = New OleDbConnection
myConnection.ConnectionString = connString
' create a data adapter
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select * from Customers",
myConnection)

Create a DataSet Object and Fill with the data

You use Fill method of OleDbDataAdpater to fill data to a DataSet object.

' create a new dataset


Dim ds As DataSet = New DataSet
' fill dataset
da.Fill(ds, "Customers")

Attach DataSet to DataGrid

Now you use DataSource method of DataGrid to attached the DataSet data to the data grid.

' Attach DataSet to DataGrid


DataGrid1.DataSource = ds.DefaultViewManager

Here is entire source code written on the form load method.

Imports System.Data
Imports System.Data.OleDb
' some code here
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
' create a connection string
Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\\Northwind.mdb"
Dim myConnection As OleDbConnection = New OleDbConnection
myConnection.ConnectionString = connString
' create a data adapter
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select * from Customers",
myConnection)
' create a new dataset
Dim ds As DataSet = New DataSet
' fill dataset
da.Fill(ds, "Customers")
' write dataset contents to an xml file by calling WriteXml method
' Attach DataSet to DataGrid
DataGrid1.DataSource = ds.DefaultViewManager
End Sub
End Class  

The output of the program looks like following figure.

You might also like