In this article I will explain with an example, how to select (check) one (single) CheckBox in Windows Forms DataGridView using C# and VB.Net.
By default, multiple CheckBoxes in DataGridView are meant for multiple selection, thus in order to make it work as single selection i.e. only one (single) CheckBox can be selected at a time, when one CheckBox is checked, all the other CheckBoxes must be unchecked.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
Form Design
You will need to add a DataGridView control to the Windows Form.
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
Select (Check) one (single) 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, a loop is executed over all the row CheckBoxes and all CheckBoxes other than the selected (checked) CheckBox are 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 and uncheck all other CheckBoxes.
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Index == e.RowIndex)
{
row.Cells["checkBoxColumn"].Value = !Convert.ToBoolean(row.Cells["checkBoxColumn"].EditedFormattedValue);
}
else
{
row.Cells["checkBoxColumn"].Value = false;
}
}
}
}
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 and uncheck all other CheckBoxes.
For Each row As DataGridViewRow In dataGridView1.Rows
If row.Index = e.RowIndex Then
row.Cells("checkBoxColumn").Value = Not Convert.ToBoolean(row.Cells("checkBoxColumn").EditedFormattedValue)
Else
row.Cells("checkBoxColumn").Value = False
End If
Next
End If
End Sub
Screenshot
Downloads