Skip to main content

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; }    
    6. }  
    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:
  1. 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:



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:
Conclusion
We have learned three ways to add a checkbox to a DataGridView.

Comments

  1. Hello , is there a way to do this,
    2 data tables
    Datatable 1
    col1, col 2
    AB, 123
    BA,1234
    AC,12A
    CA,12B

    DataTable 2
    Col1 Col 2
    AC, 11A
    AC,11B
    AC,12A
    CA,13B
    CA,12B
    CA,14B
    CA,15B

    I want a grid view to load
    with value in data table 1
    but also add 1 extra columns on the grid view,
    The column can be text box or a combo box. This has to be dynamic,
    by doing a lookup of col1 from datatable 1 to col 1 of data table 2 if the col 1 matches then third col in grid view is a col 2 value of data table 2 as a drop down , else the 3rd column on grid view is a text box column with no value.. ,
    how can i do this, i am working on winform..

    thank you sir....

    ReplyDelete

Post a Comment

Contact Form

Name

Email *

Message *

Popular posts from this blog

Exporting DataTable to Excel in C# Using Interop

Introduction I would like to share a utility that can be used to export a datatable to Excel file using C#. We have various approaches to perform this activity. In ASP.NET we can do this by changing the Content type to xls but in C# .Net we might have various other approaches. Through this article we will learn how to export a data table to Excel using Interop. Code and Steps Here in this application we will use a Sample datatable and then learn how to export data to an Excel file. We will learn the following things in this article: Creating Excel file using C# Writing data to cells Formatting data to cells Working with Excel range 1. Adding References First we need to add a reference for Microsoft.office.interop.Excel as in the following: 2. Adding sample data table to the code Use the following code to add the sample data table:     static   DataTable  GetTable()     {       ...

Binding CheckBoxList in ASP.Net MVC

Blogspot Introduction I would like to share how to bind the checkbox list in MVC. I will use a Model (a class file) to define various attributes for checkboxes. For a basic understanding of MVC kindly use the following link: ASP.NET MVC Overview The following is the procedure. 1. Creating Model We created a "SubjectModel" class under the Models folder and defined the following two properties: Subject: to display text Selected: to display check/uncheck 2. Creating the controller We created a Controller "BindingCheckBoxController" under the controllers folder We created an Action having the name = "DisplaycheckBoxes" We created a list of Subjects (Subject model class) Returning a list of subjects to the View. public  ActionResult DisplayCheckboxes() {     List<SubjectModel> listsubject =  new  List<SubjectModel>();     listsubject.Add( new  SubjectModel( "Physics" , true )); ...

Indexes In SQL SERVER in Hindi Part 1