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.
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; }
}
We have added isMarried as a
Boolean property.
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.
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));
Here we defined a bool
column, IsMarried.
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:
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:
No comments:
Post a Comment