Introduction
When working with DataGridView we might have come
across situations where we need to add a CheckBox column to the Datagridview
column. In this blog we will learn how to add a checkbox to the DataGridView
columns without using the DataGridViewCheckBoxColumn class

Binding the DataGridView
To
bind the DataGridView with data here, I have used a DataTable class to add rows
and binding the DataGridView using DataSource property.
private void btn_load_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Select", System.Type.GetType("System.Boolean"));
dt.Columns.Add("Employee No");
dt.Columns.Add("Employee Name");
dt.Columns.Add("Join Date");
DataRow dr;
for (int i = 0; i <= 10; i++)
{
dr = dt.NewRow();
dr["Select"] = false;
dr["Employee No"] = 1000 + i;
dr["Employee Name"] = "Employee " + i;
dr["Join Date"] = DateTime.Now.ToString("dd/MM/yyyy");
dt.Rows.Add(dr);
}
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dataGridView1.DataSource = dt;
}
Here we create an Instance of DataTable class and add Columns Select, Employee No, Employee Name, Join Date.
The Datatype of the Column Select is Boolean DataType which is either true/false.
Note : If the datatype of the column is Boolean in DataGridView, an CheckBox is automatically added to it rows. This does the Trick !!
Next, we add rows to the DataTable using NewRow() of the DataTable.
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
Here we set some of the properties of the DataGridview Control and finally assigning the DataSource using.
dataGridView1.DataSource = dt;
CheckBox Click Method
Here we check the Status of the CheckBox that is Checked or UnChecked. The CellContentClick event is raised when click on the Cell content.
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == dataGridView1.Columns["Select"].Index)
{
dataGridView1.EndEdit(); //Stop editing of cell.
if ((bool)dataGridView1.Rows[e.RowIndex].Cells["Select"].Value)
MessageBox.Show("The Value is Checked","Status",MessageBoxButtons.OK,MessageBoxIcon.Information);
else
MessageBox.Show("UnChecked","Status",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
In this code, e.ColumnIndex == dataGridView1.Columns["Select"].Index
To check that we are in the right column. Here the CheckBox column with Index 0.
if ((bool)dataGridView1.Rows[e.RowIndex].Cells["Select"].Value)
MessageBox.Show("The Value is Checked","Status",MessageBoxButtons.OK,MessageBoxIcon.Information);
else
MessageBox.Show("UnChecked","Status",MessageBoxButtons.OK,MessageBoxIcon.Information);
This piece of code checks that the Check box is Checked or Not and displays a message.
ScreenShot

Conclusion
In this blog we have learned how to add check box to datagridview column with out using the DataGridviewCheckBoxColumn in C# Windows Forms and checking the Status of the CheckBox.

Henry GuardadoPosted Oct 2, 2018, 10:27 AM
How I can do the same with SQL server and C#?
Generous FSPosted Aug 25, 2017, 12:36 PM
Does't work well: checkbox turns back to checked after leaving the cell. You may be need to add mouse up to commit the change. Do you?
Sandeep SalunkePosted Apr 6, 2016, 9:53 AM
thank you very much
Amol SontakkePosted Sep 28, 2015, 6:49 AM
Thank you
sanjay sharmaPosted Jan 30, 2015, 6:58 AM
Thank U sir it is a very very valuable post
Kalaivanan EPosted Nov 7, 2014, 7:12 AM
thk you
sumon banerjeePosted Jul 12, 2013, 6:42 AM
Thak you for your valuable post