In this article I will explain with an example, how to implement Checked event for CheckBox in Windows Forms DataGridView using C# and VB.Net.
By default, there is no checked event for CheckBox in Windows Forms DataGridView and thus, the same is accomplished by making use of the DataGridView CellContentClick event handler using C# and VB.Net.
 
 
Database
I have made use of the following table Customers with the schema as follows.
Implement Checked event for CheckBox in DataGridView using C# and VB.Net
 
I have already inserted few records in the table.
Implement Checked event for CheckBox in DataGridView 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.
Implement Checked event for CheckBox in DataGridView 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.
Then 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.
C#
private const string ConnectionString = @"Data Source=.\SQL2017;Initial Catalog=AjaxSamples;Integrated Security = true";
private void Form1_Load(object sender, EventArgs e)
{
    this.BindGrid();
}
 
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 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=.\SQL2017;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 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 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
 
 
Implementing Checked event for CheckBox in DataGridView
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 and if the CheckBox is checked, the value of the ID column is displayed using Message Box.
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)
    {
        //Reference the GridView Row.
        DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
 
        //Set the CheckBox selection.
        row.Cells["checkBoxColumn"].Value = !Convert.ToBoolean(row.Cells["checkBoxColumn"].EditedFormattedValue);
 
        //If CheckBox is checked, display Message Box.
        if (Convert.ToBoolean(row.Cells["checkBoxColumn"].Value))
        {
            MessageBox.Show("Selected ID: " + row.Cells[1].Value);
        }
    }
}
 
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
 
        'Reference the GridView Row.
        Dim row As DataGridViewRow = dataGridView1.Rows(e.RowIndex)
 
        'Set the CheckBox selection.
        row.Cells("checkBoxColumn").Value = Convert.ToBoolean(row.Cells("checkBoxColumn").EditedFormattedValue)
 
        'If CheckBox is checked, display Message Box.
        If Convert.ToBoolean(row.Cells("checkBoxColumn").Value) Then
            MessageBox.Show(("Selected ID: " & row.Cells(1).Value))
        End If
    End If
End Sub
 
 
Screenshot
Implement Checked event for CheckBox in DataGridView using C# and VB.Net
 
 
Downloads