How to use Gridview with checkbox control using asp.net


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

In this article I have explained about How to use Gridview with checkbox control using asp.net.
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<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(vitem.id != vChk && vitem.type=="checkbox")

{

if(vitem.checked!= varIsChecked)

{

vitem.click();

}

}

}

}


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

<Columns>

<asp:TemplateField HeaderText="Roles">

<HeaderTemplate>

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

</HeaderTemplate>

<ItemTemplate>

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

</ItemTemplate>

</asp:TemplateField>

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

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



</Columns>



</asp:GridView>
</form>
</body>
</html>
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";
dtNewTable.Columns.Add(dtcol);
dtcol1.DataType = System.Type.GetType("System.String");
dtcol1.ColumnName = "EmpName";
dtNewTable.Columns.Add(dtcol1);
int i;
for (i = 0; i < 4; i++)
{


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

GridView1.DataSource = dtNewTable;
GridView1.DataBind();
}

}

}

}


Comments

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:
    Email: