In this article I will explain with an example, how to bind (fill) DataGridView control in Windows Forms (WinForms) Application using
DataTable in C# and VB.Net.
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
Form Design
The following Form consists of:
DataGridView – For displaying records.
Adding ConnectionString to the App.Config file
You will need to add the Connection String in the ConnectionStrings section in the App.Config file as shown below.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="constr" connectionString="Data Source=.\SQL2019;Initial Catalog=Northwind;Integrated Security=true" />
</connectionStrings>
</configuration>
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Binding data to DataGridView from Database using DataTable in C# and VB.Net
Inside the Form Load event handler, the BindGrid method is called.
Inside the BindGrid method, the Connection String is fetched from the App.Config file and an object of SqlConnection class is created using it.
The SqlDataAdapter object is initialized and the SELECT query is passed to it as parameter and the connection is opened.
After that, using the
Fill function, the
DataTable is populated with the records from the database.
Finally, the
DataTable is assigned to the
DataSource property of the
DataGridView and the
DataGridView is populated.
C#
private void Form1_Load(object sender, EventArgs e)
{
this.BindGrid();
}
//Bind All Columns.
private void BindGrid()
{
string sql = "SELECT * FROM Customers";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter(sql, con))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}
}
VB.Net
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.BindGrid()
End Sub
'Bind All Columns.
Private Sub BindGrid()
Dim sql As String = "SELECT * FROM Customers"
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using sda As New SqlDataAdapter(sql, con)
Using dt As New DataTable()
sda.Fill(dt)
dataGridView1.DataSource = dt
End Using
End Using
End Using
End Sub
Binding Specific (Certain) columns to DataGridView control using C# and VB.Net
Inside the Form Load event handler, the BindGrid method is called.
Inside the BindGrid method, the Connection String is fetched from the App.Config file and an object of SqlConnection class is created using it.
Then, SqlDataAdapter object is initialized and the SELECT query is passed to it as parameter and the connection is opened.
After that, using the
Fill function, the
DataTable is populated with the records from the database.
DataGridView has been assigned with the following properties:
Properties:
AutoGenerateColumns – For allowing default DataGridView Columns to be shown. Here it is set to FALSE (Default is TRUE).
ColumnCount – For allowing maximum number of Columns to be displayed.
DataSource – For assigning data to be populated.
While adding columns it is necessary to set the following properties:
Name: Unique Name of the DataGridView Column.
HeaderText: Header Text of the DataGridView Column.
DataPropertyName: Name of the Data Column Field that will be displayed in the DataGridView Column.
Finally, the
DataTable is assigned to the
DataSource property of the
DataGridView and the
DataGridView is populated.
C#
private void Form1_Load(object sender, EventArgs e)
{
this.BindGrid();
}
//Bind Defined Columns.
private void BindGrid()
{
string sql = "SELECT CustomerID, ContactName, Country FROM Customers";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter(sql, con))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
//Set AutoGenerateColumns False.
dataGridView2.AutoGenerateColumns = false;
//Set Columns Count.
dataGridView2.ColumnCount = 3;
//Add Columns.
dataGridView2.Columns[0].Name = "CustomerId";
dataGridView2.Columns[0].HeaderText = "Customer ID";
dataGridView2.Columns[0].DataPropertyName = "CustomerID";
dataGridView2.Columns[1].HeaderText = "Contact Name";
dataGridView2.Columns[1].Name = "Name";
dataGridView2.Columns[1].DataPropertyName = "ContactName";
dataGridView2.Columns[2].Name = "Country";
dataGridView2.Columns[2].HeaderText = "Country";
dataGridView2.Columns[2].DataPropertyName = "Country";
dataGridView2.DataSource = dt;
}
}
}
}
VB.Net
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.BindGrid()
End Sub
'Bind Defined Columns.
Private Sub BindGrid()
Dim sql As String = "SELECT CustomerID, ContactName, Country FROM Customers"
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using sda As New SqlDataAdapter(sql, con)
Using dt As New DataTable()
sda.Fill(dt)
'Set AutoGenerateColumns False.
DataGridView2.AutoGenerateColumns = False
'Set Columns Count.
DataGridView2.ColumnCount = 3
'Add Columns.
DataGridView2.Columns(0).Name = "CustomerId"
DataGridView2.Columns(0).HeaderText = "Customer ID"
DataGridView2.Columns(0).DataPropertyName = "CustomerID"
DataGridView2.Columns(1).Name = "Name"
DataGridView2.Columns(1).HeaderText = "Contact Name"
DataGridView2.Columns(1).DataPropertyName = "ContactName"
DataGridView2.Columns(2).Name = "Country"
DataGridView2.Columns(2).HeaderText = "Country"
DataGridView2.Columns(2).DataPropertyName = "Country"
DataGridView2.DataSource = dt
End Using
End Using
End Using
End Sub
Screenshots
Displaying all records
Displaying specific records
Downloads