Bind Checkbox column in Datagridview based on Datatable
In this article, I will explain how to Bind Checkbox column in Datagridview with datatable. First i am creating a datatable and adding some sample data and then i am binding the datatable to datagridview. I am also binding checkbox column to the datagridview and based on datatable values the checkbox values will be shown.
Bind Checkbox column in Datagridview based on Datatable
First i am creating a datatable and adding some sample data and then i am binding the datatable to datagridview. I am also binding checkbox column to the datagridview and based on datatable values the checkbox values will be shown.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Permission
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataTable dtable = new DataTable();
int globalcheckboxcolumn = 0;
private void Form1_Load(object sender, EventArgs e)
{
//Create Checkbox column in datagridview
//Following code is to create datatable to bind the data into the datagridview
DataTable dt = new DataTable();
dt.Columns.Add("Empname");
dt.Columns.Add("Asp.net");
dt.Columns.Add("Sql");
dt.Columns.Add("C#");
dt.Columns.Add("VB");
DataRow dr = dt.NewRow();
dr["Empname"] = "Deva";
dr["Asp.net"] = true;
dr["Sql"] = false;
dr["C#"] = true;
dr["VB"] = false;
dt.Rows.Add(dr);
DataRow dr1 = dt.NewRow();
dr1["Empname"] = "Soma";
dr1["Asp.net"] = true;
dr1["Sql"] = false;
dr1["C#"] = true;
dr1["VB"] = false;
dt.Rows.Add(dr1);
//datatable is binded into the datagridview by setting dt to the datasource property of the datagridview
dtable = dt;
dataGridView1.DataSource = dtable;
//hiding the datatable columns in gridview and it will show only inserted checkbox columns to the user.
dataGridView1.Columns["Asp.net"].Visible = false;
dataGridView1.Columns["Sql"].Visible = false;
dataGridView1.Columns["C#"].Visible = false;
dataGridView1.Columns["VB"].Visible = false;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
//Binding checkbox values either true or false by checking the datatable data if is true it will check the checkbox or else it will uncheck the checkbox values.
try
{
//Checking for the datatable data of column Asp.net if it is true then set checkboxcolumn chkAspnet of the datagridviw to true else false.
if (row.Cells["Asp.net"].Value.ToString() == "True")
{
DataGridViewCheckBoxCell checkBox = (DataGridViewCheckBoxCell)row.Cells["chkAspnet"];
checkBox.Value = true;
}
else
{
DataGridViewCheckBoxCell checkBox = (DataGridViewCheckBoxCell)row.Cells["chkAspnet"];
checkBox.Value = false;
}
//Checking for the datatable data of column Sql if it is true then set checkboxcolumn chkSql of the datagridviw to true else false.
if (row.Cells["Sql"].Value.ToString() == "True")
{
DataGridViewCheckBoxCell checkBox = (DataGridViewCheckBoxCell)row.Cells["chkSql"];
checkBox.Value = true;
}
else
{
DataGridViewCheckBoxCell checkBox = (DataGridViewCheckBoxCell)row.Cells["chkSql"];
checkBox.Value = false;
}
//Checking for the datatable data of column C# if it is true then set checkboxcolumn chkC of the datagridviw to true else false.
if (row.Cells["C#"].Value.ToString() == "True")
{
DataGridViewCheckBoxCell checkBox = (DataGridViewCheckBoxCell)row.Cells["chkC#"];
checkBox.Value = true;
}
else
{
DataGridViewCheckBoxCell checkBox = (DataGridViewCheckBoxCell)row.Cells["chkC#"];
checkBox.Value = false;
}
//Checking for the datatable data of column VB if it is true then set checkboxcolumn chkVB of the datagridviw to true else false.
if (row.Cells["VB"].Value.ToString() == "True")
{
DataGridViewCheckBoxCell checkBox = (DataGridViewCheckBoxCell)row.Cells["chkVB"];
checkBox.Value = true;
}
else
{
DataGridViewCheckBoxCell checkBox = (DataGridViewCheckBoxCell)row.Cells["chkVB"];
checkBox.Value = false;
}
}
catch
{
}
}
}
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
//globalcheckboxcolumn global variable is used insert checkbox column only once into the datagridview
if (globalcheckboxcolumn == 0)
{
//Following code checking only if datagridviw has rows only checkbox rows will be inserted.
if (dataGridView1.Rows.Count != 0)
{
//Inserting four CheckBoxColumn in DataGridView are Aspnet,Sql,C#,VB
//Creating an object chkSelect of DataGridViewCheckBoxColumn with name as chkAspnet to insert into the datagridview
DataGridViewCheckBoxColumn chkSelect = new DataGridViewCheckBoxColumn();
{
chkSelect.HeaderText = "Aspnet";
chkSelect.Name = "chkAspnet";
chkSelect.Selected = false;
}
dataGridView1.Columns.Insert(2, chkSelect);
//Creating an object chkSql of DataGridViewCheckBoxColumn with name as chkSql to insert into the datagridview
DataGridViewCheckBoxColumn chkSql = new DataGridViewCheckBoxColumn();
{
chkSql.HeaderText = "Sql";
chkSql.Name = "chkSql";
chkSql.Selected = false;
}
dataGridView1.Columns.Insert(3, chkSql);
//Creating an object chkC# of DataGridViewCheckBoxColumn with name as chkC# to insert into the datagridview
DataGridViewCheckBoxColumn chkC# = new DataGridViewCheckBoxColumn();
{
chkC#.HeaderText = "C#";
chkC#.Name = "chkC#";
chkC#.Selected = false;
}
dataGridView1.Columns.Insert(4, chkC#);
//Creating an object chkVB of DataGridViewCheckBoxColumn with name as chkVB to insert into the datagridview
DataGridViewCheckBoxColumn chkVB = new DataGridViewCheckBoxColumn();
{
chkVB.HeaderText = "VB";
chkVB.Name = "chkVB";
chkVB.Selected = false;
}
dataGridView1.Columns.Insert(5, chkVB);
}
globalcheckboxcolumn++;
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//while clicking the checkbox in gridview it will be saved in the datatable by the following code.
string value = dataGridView1.Columns[e.ColumnIndex].Name;
if (value == "chkAspnet")
{
DataGridViewCheckBoxCell chkAspnet = new DataGridViewCheckBoxCell();
chkAspnet = (DataGridViewCheckBoxCell)dataGridView1.Rows[dataGridView1.CurrentRow.Inde].Cells[0];
if (chkAspnet.Value == null)
chkAspnet.Value = false;
switch (chkAspnet.Value.ToString())
{
case "True":
chkAspnet.Value = false;
break;
case "False":
chkAspnet.Value = true;
break;
}
if (chkAspnet.Value.ToString() == "True")
{
//Storing true value in datatable dtable for the column Asp.net if it is changed
dtable.Rows[e.RowIndex]["Asp.net"] = true;
}
else
{
//Storing false value in datatable dtable for the column Asp.net if it is changed
dtable.Rows[e.RowIndex]["Asp.net"] = false;
}
}
else if (value == "chkSql")
{
DataGridViewCheckBoxCell chkSql = new DataGridViewCheckBoxCell();
chkSql = (DataGridViewCheckBoxCell)dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1];
if (chkSql.Value == null)
chkSql.Value = false;
switch (chkSql.Value.ToString())
{
case "True":
chkSql.Value = false;
break;
case "False":
chkSql.Value = true;
break;
}
if (chkSql.Value.ToString() == "True")
{
//Storing true value in datatable dtable for the column Sql if it is changed
dtable.Rows[e.RowIndex]["Sql"] = true;
}
else
{
//Storing false value in datatable dtable for the column Sql if it is changed
dtable.Rows[e.RowIndex]["Sql"] = false;
}
}
else if (value == "chkC#")
{
DataGridViewCheckBoxCell chkC = new DataGridViewCheckBoxCell();
chkC = (DataGridViewCheckBoxCell)dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[2];
if (chkC.Value == null)
chkC.Value = false;
switch (chkC.Value.ToString())
{
case "True":
chkC.Value = false;
break;
case "False":
chkC.Value = true;
break;
}
if (chkC.Value.ToString() == "True")
{
//Storing false value in datatable dtable for the column C# if it is changed
dtable.Rows[e.RowIndex]["C#"] = true;
}
else
{
//Storing false value in datatable dtable for the column C# if it is changed
dtable.Rows[e.RowIndex]["C#"] = false;
}
}
else if (value == "chkVB")
{
DataGridViewCheckBoxCell chkVB = new DataGridViewCheckBoxCell();
chkVB = (DataGridViewCheckBoxCell)dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[3];
if (chkVB.Value == null)
chkVB.Value = false;
switch (chkVB.Value.ToString())
{
case "True":
chkVB.Value = false;
break;
case "False":
chkVB.Value = true;
break;
}
if (chkVB.Value.ToString() == "True")
{
//Storing false value in datatable dtable for the column VB if it is changed
dtable.Rows[e.RowIndex]["VB"] = true;
}
else
{
//Storing false value in datatable dtable for the column VB if it is changed
dtable.Rows[e.RowIndex]["VB"] = false;
}
}
}
}
}
I had attached sample code for reference.
To bind the DataGridView with data here, I have used a DataTable class to add rows and binding the DataGridView using DataSource property.