Displaying data in a DataGridView control using Dataset and DataAdapter in Visual Basic.Net
This article explains how to display data in a DataGridView control using DataSet. In this sample program, we will use a SQL Server database, FinAccounting and we will access Accounts table of FinAccounting database and display records in the datagridview control.

Datasets are part of the DataAccess Layer. How do you use Dataset object in a DataAccess Layer of a database application? Preparation:
Create a Windows form and add a DataGridView control to the form. Tasks:
myDataSet = New DataSet()
Once we have set up the DataAdapter and the DataSet, we need to populate the DataSet. The Fill method of the DataAdapter class is used for populating and refreshing the DataSet object.
Program Code
Summary:
This program demonstrates how to access data using a DataAdapter and store it in a dataset. This DataSet is attached to the datagridview control and displayed on the form.

Datasets are part of the DataAccess Layer. How do you use Dataset object in a DataAccess Layer of a database application? Preparation:
Create a Windows form and add a DataGridView control to the form. Tasks:
- Declare and setup the Connection string
- Declare and build a query string
- Create SqlDataAdapter Object
- Create a DataSet Object and Fill it with data
- Bind DataSet to the DataGridView control
Declare and setup the Connection string
This task declares, instantiates and sets up the connection. We need a connection when we want to access a data source. We will use a connection string to set up the connection with the database. We have used the SqlConnection class which is only to be used with MS SQL Server database as it has been specially optimized for connecting to the SQL Server database.Declare and build a query string
All query statements used in the program are declared in this section and assigned to string variables.Create SqlDataAdapter Object
The DataAdapter is used for retrieving data from the database and populating the dataset. The DataAdapter is the connector class that sits between the disconnected and the connected parts of ADO.Net. The DataAdapter class is instantiated using the New constructor, which is overloaded. If we use the default constructor for the DataAdapter, we need to specify the Command object for the actions performed. This means that if we want to retrieve rows from the Data Source, we will have to set the Select command property.Create a DataSet Object and Fill it with Accounts data
Unlike the managed provider objects, the DataSet object do not diverge between the OleDb and SqlClient .net namespaces. We declare a DataSet object the same way regardless of which .NET data provider we are using:myDataSet = New DataSet()
Once we have set up the DataAdapter and the DataSet, we need to populate the DataSet. The Fill method of the DataAdapter class is used for populating and refreshing the DataSet object.
Attach DataSet to DataGrid
Now we use DataSource method of DataGridView control to attach the DataSet data to the datagridview control.Program Code
Summary:
This program demonstrates how to access data using a DataAdapter and store it in a dataset. This DataSet is attached to the datagridview control and displayed on the form.
Imports System.Data.SqlClient Public Class Form1 'Declare the string variable 'connectionString' to hold the ConnectionString Dim connectionString As String = "server=SYS2;" + "integrated security=SSPI;" + "database=FinAccounting" 'Declare the string variable 'str_Account_Select' to hold the SQL statement Dim str_Account_Select As String = "SELECT * FROM AccountsTable " Dim myConnection As SqlConnection Dim myCommand As SqlCommand Dim myAdapter As SqlDataAdapter 'Declare the DataSet object Dim myDataSet As DataSet Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Instantiate the Connection object myConnection = New SqlConnection(connectionString) 'Instantiate the Command object myCommand = New SqlCommand(str_Account_Select, myConnection) myConnection.Open() 'Instantiate DataSet object myDataSet = New DataSet() 'Instantiate DataAdapter object myAdapter = New SqlDataAdapter() 'Set DataAdapter command properties myAdapter.SelectCommand = myCommand 'Populate the Dataset myAdapter.Fill(myDataSet, "AccountsTable") If (myDataSet.Tables("AccountsTable").Rows.Count = 0) Then MessageBox.Show("There are currently no registries in the database.") Else DataGridView1.DataSource = myDataSet.Tables("AccountsTable") End If End Sub End Class
Visual Basic.Net Articles
- Dataset in Visual basic .net
- Ado.net data access
- Creating, Using Namespaces in visual basic.Net
- Business, Presentation and Data layers
- Implementing Structure in visual basic.Net
- access database in visual basic.net
- user custom controls in C# visual basic 2005, asp.net, application
- Using assemblies in .net applications
- How to create and sign a shared assembly
- Data access using ado .net in Visual Basic .Net
- Middle tier component for accessing sql server using visual basic .net
- Data binding | CurrencyManager and BindingContext
People also viewed:
Create your own ERP Software using ASP Core>>
Create your own Azure SOM Software>>
Create your own ERP Software using ASP .Net and SQL>>
Create your own Accounting Software using C# >>
Create your own SOM using Entity Framework MVC >>





Most Viewed
Azure Q & A
Azure Platform
Grid-View
GridView CommandField example
Details-View
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Entity Framework
Query entity data model using linq to entities
Array List
Difference between arraylist and list collection
Web Services
How to create a Web service using Visual Studio.net
Form-View
FormView DataBound Event
Object Oriented Programming
Calling base class constructor in C#
Linq
Convert a sequence to a generic list using ToList()method
Project Ideas
Project ideas for students
AccountingSoftware
Accounting Software
MVC
Creating an ASP.Net MVC 3 application
.Net
Using assemblies in .net applications
ASP .Net
How to implement form validation using ASP.Net 2.0 Validation Controls