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:
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:
- Binding a List to a DataGridView having a bool property
- Binding a datatable to a DataGridView having a bool column
- Adding a checkbox column (DataGridViewCheckBoxColumn) to the DataGridView.
Procedure
1. Binding a List to a DataGridView having a bool property.
- Add the following class to the project:
- public class Emp
- {
- public bool isMarried { get; set; }
- public string Name { get; set; }
- public string city { get; set; }
- }
- Creating a List of Emp classes:
- List<Emp> eObj = new List<Emp>();
- Emp emp = new Emp();
- emp.isMarried = false;
- emp.Name = "Devesh";
- emp.city = "Noida";
- eObj.Add(emp);
- emp = new Emp();
- emp.isMarried = false;
- emp.Name = "ROLI";
- emp.city = "MAINPURI";
- eObj.Add(emp);
- emp = new Emp();
- emp.isMarried = true;
- emp.Name = "Manish";
- emp.city = "GZB";
- eObj.Add(emp);
- emp = new Emp();
- emp.isMarried = false;
- emp.Name = "Nikhil";
- emp.city = "NOIda";
- eObj.Add(emp);
- Binding DataGGridView to a List:
- DataGGridView1.DataSource = eObj;
- Running the code:
- 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.
- Add a datatable to code:
- DataTable dtEmp = new DataTable();
- // add column to datatable
- dtEmp.Columns.Add("IsMarried", typeof(bool));
- dtEmp.Columns.Add("EmpID", typeof(int));
- dtEmp.Columns.Add("EmpName", typeof(string));
- dtEmp.Columns.Add("EmpCity", typeof(string));
- Adding data:
- dtEmp.Rows.Add(false, 111, "Devesh", "GZB");
- dtEmp.Rows.Add(false, 222, "ROLI", "KANPUR");
- dtEmp.Rows.Add(true, 333, "Rajesh", "NOIDa");
- dtEmp.Rows.Add(false,444, "NIKHIL", "KANPUR");
- Binding Grid:
- DataGGridView1.DataSource = dtEmp;
- 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.
- Add a datatable without having a bool column:
- DataTable dtEmp = new DataTable();
- // add column to datatable
- dtEmp.Columns.Add("EmpID", typeof(int));
- dtEmp.Columns.Add("EmpName", typeof(string));
- dtEmp.Columns.Add("EmpCity", typeof(string));
- Adding data:
- dtEmp.Rows.Add(111, "Devesh", "GZB");
- dtEmp.Rows.Add( 222, "ROLI", "KANPUR");
- dtEmp.Rows.Add(333, "Rajesh", "NOIDa");
- dtEmp.Rows.Add( 444, "NIKHIL", "KANPUR");
- Binding data:
- DataGGridView1.DataSource = dtEmp;
- Adding checkbox column
- DataGGridViewCheckBoxColumn dgvCmb = new DataGGridViewCheckBoxColumn();
- dgvCmb.ValueType = typeof(bool);
- dgvCmb.Name = "Chk";
- dgvCmb.HeaderText = "CheckBox";
- DataGGridView1.Columns.Add(dgvCmb);
- Running the code:
Conclusion
We have learned three ways to add a checkbox to a DataGridView.
We have learned three ways to add a checkbox to a DataGridView.
Hello , is there a way to do this,
ReplyDelete2 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....