In this article I will explain with an example, how to check all (select all) and uncheck all (deselect all) CheckBoxes in DataGridView in Windows Forms Application using C# and VB.Net.
 
 
Database
I have made use of the following table Customers with the schema as follows.
Check all and Uncheck all CheckBox in DataGridView in Windows Application using C# and VB.Net
 
I have already inserted few records in the table.
Check all and Uncheck all CheckBox in DataGridView in Windows Application using C# and VB.Net
 
Note: You can download the database table SQL by clicking the download link below.
         Download SQL file
 
 
Form Design
You will need to add a DataGridView control to the Windows Form.
Check all and Uncheck all CheckBox in DataGridView in Windows Application using C# and VB.Net
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
 
VB.Net
Imports System.Data
Imports System.Data.SqlClient
 
 
Populating the DataGridView from Database
The DataGridView is populated inside the Form Load event where the BindGrid method is called which populates records from the Customers table and populates the DataGridView control.
After the DataGridView is populated from database, first a CheckBox is created and added to the DataGridView. This CheckBox will act as the Header CheckBox and since CheckBox cannot be added to Header row, it is placed within the first Header cell after determining its Location.
A Click event handler is attached to the Header CheckBox to handle the check all and uncheck all functionality of the row CheckBoxes of the DataGridView.
Finally a DataGridViewCheckBoxColumn is created and added at the 0th Index in the DataGridView control.
And since the DataGridViewCheckBoxColumn does not have any Click event handler, a CellContentClick event handler is attached to the DataGridView to handle check and uncheck of Header CheckBox when all the row CheckBoxes are checked or unchecked respectively.
C#
private const string ConnectionString = @"Data Source=.\SQL2014;Initial Catalog=AjaxSamples;Integrated Security = true";
private void Form1_Load(object sender, EventArgs e)
{
    this.BindGrid();
}
 
CheckBox headerCheckBox = new CheckBox();
private void BindGrid()
{
    using (SqlConnection con = new SqlConnection(ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con))
        {
            cmd.CommandType = CommandType.Text;
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    dataGridView1.DataSource = dt;
                }
            }
        }
    }
 
    //Add a CheckBox Column to the DataGridView Header Cell.
 
    //Find the Location of Header Cell.
    Point headerCellLocation = this.dataGridView1.GetCellDisplayRectangle(0, -1, true).Location;
 
    //Place the Header CheckBox in the Location of the Header Cell.
    headerCheckBox.Location = new Point(headerCellLocation.X + 8, headerCellLocation.Y + 2);
    headerCheckBox.BackColor = Color.White;
    headerCheckBox.Size = new Size(18, 18);
 
    //Assign Click event to the Header CheckBox.
    headerCheckBox.Click += new EventHandler(HeaderCheckBox_Clicked);
    dataGridView1.Controls.Add(headerCheckBox);
 
    //Add a CheckBox Column to the DataGridView at the first position.
    DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
    checkBoxColumn.HeaderText = "";
    checkBoxColumn.Width = 30;
    checkBoxColumn.Name = "checkBoxColumn";
    dataGridView1.Columns.Insert(0, checkBoxColumn);
 
    //Assign Click event to the DataGridView Cell.
    dataGridView1.CellContentClick += new DataGridViewCellEventHandler(DataGridView_CellClick);
}
 
VB.Net
Private Const ConnectionString As String = "Data Source=.\SQL2014;Initial Catalog=AjaxSamples;Integrated Security = true"
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Me.BindGrid()
End Sub
 
Private headerCheckBox As CheckBox = New CheckBox()
Private Sub BindGrid()
    Using con As SqlConnection = New SqlConnection(ConnectionString)
        Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con)
            cmd.CommandType = CommandType.Text
            Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
                Using dt As DataTable = New DataTable()
                    sda.Fill(dt)
                    dataGridView1.DataSource = dt
                End Using
            End Using
        End Using
    End Using
 
    'Add a CheckBox Column to the DataGridView Header Cell.
 
    'Find the Location of Header Cell.
    Dim headerCellLocation As Point = Me.dataGridView1.GetCellDisplayRectangle(0, -1, True).Location
 
    'Place the Header CheckBox in the Location of the Header Cell.
    headerCheckBox.Location = New Point(headerCellLocation.X + 8, headerCellLocation.Y + 2)
    headerCheckBox.BackColor = Color.White
    headerCheckBox.Size = New Size(18, 18)
 
    'Assign Click event to the Header CheckBox.
    AddHandler headerCheckBox.Click, AddressOf HeaderCheckBox_Clicked
    dataGridView1.Controls.Add(headerCheckBox)
 
    'Add a CheckBox Column to the DataGridView at the first position.
    Dim checkBoxColumn As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn()
    checkBoxColumn.HeaderText = ""
    checkBoxColumn.Width = 30
    checkBoxColumn.Name = "checkBoxColumn"
    dataGridView1.Columns.Insert(0, checkBoxColumn)
    'Assign Click event to the DataGridView Cell.
    AddHandler dataGridView1.CellContentClick, AddressOf DataGridView_CellClick
End Sub
 
 
Check uncheck all row CheckBoxes when the Header CheckBox is checked or unchecked
When the Header Row CheckBox is clicked the following event handler is executed, where a loop is executed over the DataGridView rows.
Inside the loop, the checked value of the Header CheckBox is assigned to each row CheckBox of the DataGridView.
C#
private void HeaderCheckBox_Clicked(object sender, EventArgs e)
{
    //Necessary to end the edit mode of the Cell.
    dataGridView1.EndEdit();
 
    //Loop and check and uncheck all row CheckBoxes based on Header Cell CheckBox.
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        DataGridViewCheckBoxCell checkBox = (row.Cells["checkBoxColumn"] as DataGridViewCheckBoxCell);
        checkBox.Value = headerCheckBox.Checked;
    }
}
 
VB.Net
Private Sub HeaderCheckBox_Clicked(ByVal sender As Object, ByVal e As EventArgs)
    'Necessary to end the edit mode of the Cell.
    dataGridView1.EndEdit()
 
    'Loop and check and uncheck all row CheckBoxes based on Header Cell CheckBox.
    For Each row As DataGridViewRow In dataGridView1.Rows
        Dim checkBox As DataGridViewCheckBoxCell = (TryCast(row.Cells("checkBoxColumn"), DataGridViewCheckBoxCell))
        checkBox.Value = headerCheckBox.Checked
    Next
End Sub
 
 
Check uncheck the Header CheckBox when all the row CheckBoxes checked or unchecked
When any of the row CheckBox is clicked, the DataGridView CellContentClick event handler is executed.
Inside this event handler, first a check is made whether the clicked Cell belongs to first column and does not belong to Header row.
If the above condition is successful, a loop is executed over all the row CheckBoxes to make sure whether all the row CheckBoxes are checked or not.
If all the row CheckBoxes are checked, the Header CheckBox is set as checked. If at-least one of the row CheckBox is unchecked, then the Header CheckBox is set as unchecked.
C#
private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
    //Check to ensure that the row CheckBox is clicked.
    if (e.RowIndex >= 0 && e.ColumnIndex == 0)
    {
        //Loop to verify whether all row CheckBoxes are checked or not.
        bool isChecked = true;
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if (Convert.ToBoolean(row.Cells["checkBoxColumn"].EditedFormattedValue) == false)
            {
                isChecked = false;
                break;
            }
        }
        headerCheckBox.Checked = isChecked;
    }
}
 
VB.Net
Private Sub DataGridView_CellClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
    'Check to ensure that the row CheckBox is clicked.
    If e.RowIndex >= 0 AndAlso e.ColumnIndex = 0 Then
 
        'Loop to verify whether all row CheckBoxes are checked or not.
        Dim isChecked As Boolean = True
        For Each row As DataGridViewRow In dataGridView1.Rows
            If Convert.ToBoolean(row.Cells("checkBoxColumn").EditedFormattedValue) = False Then
                isChecked = False
                Exit For
            End If
        Next
 
        headerCheckBox.Checked = isChecked
    End If
End Sub
 
 
Screenshot
Check all and Uncheck all CheckBox in DataGridView in Windows Application using C# and VB.Net
 
 
Downloads