Adding CheckBox Column in DataGridView in C# Window Forms

Introduction

I would like to share three ways to add a checkbox to a DataGridView in a C# Windows Forms application.

We will learn the following three ways to add a checkbox to a DataGridView:

  1. Binding a List to a DataGridView having a bool property
  2. Binding a datatable to a DataGridView having a bool column
  3. Adding a checkbox column (DataGridViewCheckBoxColumn) to the DataGridView.

Procedure

1. Binding a List to a DataGridView having a bool property.

  1. Add the following class to the project:
    1. public class Emp  
    2. {  
    3.    public bool isMarried { getset; }  
    4.    public string Name { getset; }  
    5.    public string city { getset; }   

    We have added isMarried as a Boolean property.
     
  2. Creating a List of Emp classes:
    1. List<Emp> eObj = new List<Emp>();  
    2.   
    3. Emp emp = new Emp();  
    4. emp.isMarried = false;  
    5. emp.Name = "Devesh";  
    6. emp.city = "Noida";  
    7. eObj.Add(emp);  
    8. emp = new Emp();  
    9. emp.isMarried = false;  
    10. emp.Name = "ROLI";  
    11. emp.city = "MAINPURI";  
    12. eObj.Add(emp);  
    13.   
    14. emp = new Emp();  
    15. emp.isMarried = true;  
    16. emp.Name = "Manish";  
    17. emp.city = "GZB";  
    18. eObj.Add(emp);  
    19.   
    20. emp = new Emp();  
    21. emp.isMarried = false;  
    22. emp.Name = "Nikhil";  
    23. emp.city = "NOIda";  
    24. eObj.Add(emp); 

  3. Binding DataGGridView to a List:
    1. DataGGridView1.DataSource = eObj; 
  4. Running the code:

    Running

    We can see here a checkbox is automatically added to the DataGridView that is only due to the bool property on the Emp class.

    We did nothing to show the checkbox in the DataGridView.

2. Binding a datatable to the DataGridView having a bool column.

  1. Add a datatable to code:
    1. DataTable dtEmp = new DataTable();  
    2. // add column to datatable  
    3. dtEmp.Columns.Add("IsMarried"typeof(bool));  
    4. dtEmp.Columns.Add("EmpID"typeof(int));  
    5. dtEmp.Columns.Add("EmpName"typeof(string));  
    6. dtEmp.Columns.Add("EmpCity"typeof(string)); 
    Here we defined a bool column, IsMarried.
     
  2. Adding data:
    1. dtEmp.Rows.Add(false, 111, "Devesh""GZB");  
    2. dtEmp.Rows.Add(false, 222, "ROLI""KANPUR");  
    3. dtEmp.Rows.Add(true, 333, "Rajesh""NOIDa");  
    4. dtEmp.Rows.Add(false,444, "NIKHIL""KANPUR"); 
  3. Binding Grid:
    1. DataGGridView1.DataSource = dtEmp; 
  4. Running Code.

    We will get the following screen after running the code:

    Checkbox automatically added to datagirdview because of Bool column defined in datatables:

    datatable

3. Adding the checkbox column (DataGridViewCheckBoxColumn) to the DataGridView.

  1. Add a datatable without having a bool column:
    1. DataTable dtEmp = new DataTable();  
    2. // add column to datatable  
    3.   
    4. dtEmp.Columns.Add("EmpID"typeof(int));  
    5. dtEmp.Columns.Add("EmpName"typeof(string));  
    6. dtEmp.Columns.Add("EmpCity"typeof(string)); 
  2. Adding data:
    1. dtEmp.Rows.Add(111, "Devesh""GZB");  
    2. dtEmp.Rows.Add( 222, "ROLI""KANPUR");  
    3. dtEmp.Rows.Add(333, "Rajesh""NOIDa");  
    4. dtEmp.Rows.Add( 444, "NIKHIL""KANPUR"); 
  3. Binding data:
    1. DataGGridView1.DataSource = dtEmp; 
  4. Adding checkbox column
    1. DataGGridViewCheckBoxColumn dgvCmb = new DataGGridViewCheckBoxColumn();  
    2. dgvCmb.ValueType = typeof(bool);  
    3. dgvCmb.Name = "Chk";  
    4. dgvCmb.HeaderText = "CheckBox";  
    5. DataGGridView1.Columns.Add(dgvCmb); 
  5. Running the code:

    Running code

Conclusion

We have learned three ways to add a checkbox to a DataGridView.

Up Next
    Ebook Download
    View all
    Learn
    View all