In this article I will explain with an example, how to bind / fill / populate DataGridView control with DataSet in Windows Forms (WinForms) Application in C# and VB.Net.
 
Database
For this article I am making use of the Microsoft’s Northwind Database. Download and install instructions are provided in the link below
 
Adding a DataGridView to the Windows Form
Firstly you need to add a DataGridView control to the Windows Form from the Visual Studio ToolBox as shown below.
Bind / Fill / Populate DataGridView with DataSet in Windows Forms (WinForms) Application in C# and VB.Net
 
Namespaces
You will need to import the following namespace.
C#
using System.Data;
using System.Data.SqlClient;
 
VB.Net
Imports System.Data
Imports System.Data.SqlClient
 
 
Simple binding of data to DataGridView from Database using DataSet
Below is the simple and straight forward way of binding data to DataGridView control using DataSet.
In the below code, the DataSet is populated from Customers Table of Northwind Database and then it is set as DataSource to the DataGridView control.
In this approach all columns returned by the Select Query will be displayed in the DataGridView.
C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        BindGrid();
    }
 
    private void BindGrid()
    {
        string constring = @"Data Source=.\SQL2005;Initial Catalog=Northwind;User id = sa;password=pass@123";
        using (SqlConnection con = new SqlConnection(constring))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
            {
                cmd.CommandType = CommandType.Text;
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    using (DataSet ds = new DataSet())
                    {
                        sda.Fill(ds);
                        dataGridView1.DataSource = ds.Tables[0];
                    }
                }
            }
        }
    }
}
 
VB.Net
Public Class Form1
    Public Sub New()
        InitializeComponent()
        BindGrid()
    End Sub
 
    Private Sub BindGrid()
        Dim constring As String = "Data Source=.\SQL2005;Initial Catalog=Northwind;User id = sa;password=pass@123"
        Using con As New SqlConnection(constring)
            Using cmd As New SqlCommand("SELECT * FROM Customers", con)
                cmd.CommandType = CommandType.Text
                Using sda As New SqlDataAdapter(cmd)
                    Using ds As New DataSet()
                        sda.Fill(ds)
                        dataGridView1.DataSource = ds.Tables(0)
                    End Using
                End Using
            End Using
        End Using
    End Sub
End Class
 
Bind / Fill / Populate DataGridView with DataSet in Windows Forms (WinForms) Application in C# and VB.Net
 
Populate Specific (Certain) columns to DataGridView control using DataSet
Sometimes requirement is such that you would like to bind only some specific (certain) columns and would not like to show all columns as done in the above approach.
For such cases we need to AutoGenerateColumns property to False (default True) and add the columns you want to show in DataGridView using code.
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.
C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        BindGrid();
    }
 
    private void BindGrid()
    {
        string constring = @"Data Source=.\SQL2005;Initial Catalog=Northwind;User id = sa;password=pass@123";
        using (SqlConnection con = new SqlConnection(constring))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
            {
                cmd.CommandType = CommandType.Text;
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    using (DataSet ds = new DataSet())
                    {
                        sda.Fill(ds);
 
                        //Set AutoGenerateColumns False
                        dataGridView1.AutoGenerateColumns = false;
 
                        //Set Columns Count
                        dataGridView1.ColumnCount = 3;
 
                        //Add Columns
                        dataGridView1.Columns[0].Name = "CustomerId";
                        dataGridView1.Columns[0].HeaderText = "Customer Id";
                        dataGridView1.Columns[0].DataPropertyName = "CustomerID";
 
                        dataGridView1.Columns[1].HeaderText = "Contact Name";
                        dataGridView1.Columns[1].Name = "Name";
                        dataGridView1.Columns[1].DataPropertyName = "ContactName";
 
                        dataGridView1.Columns[2].Name = "Country";
                        dataGridView1.Columns[2].HeaderText = "Country";
                        dataGridView1.Columns[2].DataPropertyName = "Country";
                        dataGridView1.DataSource = ds.Tables[0];
                    }
                }
            }
        }
    }
}
 
VB.Net
Public Class Form1
    Public Sub New()
        InitializeComponent()
        BindGrid()
    End Sub
 
    Private Sub BindGrid()
        Dim constring As String = "Data Source=.\SQL2005;Initial Catalog=Northwind;User id = sa;password=pass@123"
        Using con As New SqlConnection(constring)
            Using cmd As New SqlCommand("SELECT * FROM Customers", con)
                cmd.CommandType = CommandType.Text
                Using sda As New SqlDataAdapter(cmd)
                    Using ds As New DataSet()
                        sda.Fill(ds)
 
                        'Set AutoGenerateColumns False
                        dataGridView1.AutoGenerateColumns = False
 
                        'Set Columns Count
                        dataGridView1.ColumnCount = 3
 
                        'Add Columns
                        dataGridView1.Columns(0).Name = "CustomerId"
                        dataGridView1.Columns(0).HeaderText = "Customer Id"
                        dataGridView1.Columns(0).DataPropertyName = "CustomerID"
 
                        dataGridView1.Columns(1).Name = "Name"
                        dataGridView1.Columns(1).HeaderText = "Contact Name"
                        dataGridView1.Columns(1).DataPropertyName = "ContactName"
 
                        dataGridView1.Columns(2).Name = "Country"
                        dataGridView1.Columns(2).HeaderText = "Country"
                        dataGridView1.Columns(2).DataPropertyName = "Country"
                        dataGridView1.DataSource = ds.Tables(0)
                    End Using
                End Using
            End Using
        End Using
    End Sub
End Class
 
Bind / Fill / Populate DataGridView with DataSet in Windows Forms (WinForms) Application in C# and VB.Net
 
Downloads