How to use Gridview with checkbox control using

In this article I have explained about How to use Gridview with checkbox control using Each steps I have clearly explained in this article.

In this article I have explained about How to use Gridview with checkbox control using
Each steps I have clearly explained in this article.You can check and uncheck checkboxes dynamically. You do not need to hard code the name of checkbox.
This code will automatically take care. While clicking Gridview header checkbox automatically column checkbox will checked or uncheked.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="" >
<head runat="server">
<title>Untitled Page</title>

<script type="text/javascript">

function SelectAllCheckboxesMoreSpecific(varspanChkbox)


var varIsChecked = varspanChkbox.checked;

var vChk = varspanChkbox;

Parent = document.getElementById('GridView1');

for(i=0;i< Parent.rows.length;i++)


var vtr = Parent.rows[i];

var vtd = vtr.firstChild;

var vitem = vtd.firstChild;

if( != vChk && vitem.type=="checkbox")


if(vitem.checked!= varIsChecked)






<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BorderWidth="1px" CellPadding="4" Width="472px">


<asp:TemplateField HeaderText="Roles">


<asp:CheckBox ID="chkSelectAll" onclick="SelectAllCheckboxesMoreSpecific(this);" runat="server" />



<asp:CheckBox ID="chkSelect" runat="server" />



<asp:BoundField DataField="EmpID" HeaderText="Emp ID" ReadOnly="True" />

<asp:BoundField DataField="EmpName" HeaderText="Emp Name" ReadOnly="True" />


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
if (!Page.IsPostBack)
DataRow dtrow;
DataTable dtNewTable;
dtNewTable = new DataTable();
DataColumn dtcol, dtcol1;
dtcol = new DataColumn();
dtcol1 = new DataColumn();
dtcol.DataType = System.Type.GetType("System.String");
dtcol.ColumnName = "EmpID";
dtcol1.DataType = System.Type.GetType("System.String");
dtcol1.ColumnName = "EmpName";
int i;
for (i = 0; i < 4; i++)

dtrow = dtNewTable.NewRow();
dtrow["EmpID"] = "";
dtrow["EmpName"] = "";

GridView1.DataSource = dtNewTable;





No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: