Adding Check Box to DataGridView Column without using DataGridViewCheckBoxColumn Class

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

Chk1.JPG
 

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

chk2.JPG
 

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.

Ebook Download
View all
Learn
View all