Design Page (.aspx file)
<%@
Page Language="C#" AutoEventWireup="true" CodeFile="gridiew.aspx.cs" Inherits="gridiew" %>
<!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 id="Head1" runat="server">
<title>Untitled Page</title>
<style
type="text/css">
.style1
{
width:
100%;
}
.style2
{
height:
15px;
}
.style3
{
}
.style4
{
width:
134px;
}
.style5
{
height:
19px;
}
</style>
</head>
<body>
<form
id="form1"
runat="server">
<div
style="height: 268px">
<table cellpadding="0" cellspacing="0" class="style1">
<tr>
<td class="style2" colspan="2">
gridview example
</td>
</tr>
<tr>
<td class="style4">
<asp:CheckBox ID="CheckBox1" runat="server" Text="Select all" AutoPostBack="True"
OnCheckedChanged="CheckBox1_CheckedChanged"
/>
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="Delete" Width="87px" OnClick="Button1_Click" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Height="50%"
Width="101%" OnSelectedIndexChanged="CheckBox1_CheckedChanged">
<Columns>
<asp:TemplateField HeaderText="Empid">
<EditItemTemplate>
<asp:TextBox ID="TextBox1"
runat="server"
Text='<%# Bind("Customer_Name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<table cellpadding="0"
cellspacing="0"
class="style1">
<tr>
<td align="left">
<asp:CheckBox ID="CheckBox1"
runat="server"
Text="Select
all" />
<asp:Button ID="Button1"
runat="server"
Text="Delete"
Width="100px"
/>
</td>
</tr>
</table>
</FooterTemplate>
<HeaderTemplate>
<table cellpadding="0"
cellspacing="0"
class="style1">
<tr>
<td class="style5">
<asp:CheckBox ID="CheckBox3"
runat="server"
AutoPostBack="True"
OnCheckedChanged="CheckBox3_CheckedChanged"
/>
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox2"
runat="server"
OnCheckedChanged="CheckBox2_CheckedChanged"
Text='<%# Eval("Customer_Name") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Customer_Name"
HeaderText="EmpName"
/>
<asp:BoundField DataField="Customer_Name"
HeaderText="Salary"
/>
<asp:TemplateField></asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td class="style3" colspan="2">
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Code Behind(.cs file)
using System;
using
System.Collections;
using
System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using
System.Web.Security;
using System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using
System.Web.Configuration;
using
System.Data.SqlClient;
public partial class gridiew : System.Web.UI.Page
{
SqlConnection cn = new SqlConnection(@"Data Source=.\SQL;Initial
Catalog=pankaj;Integrated Security=true");
string chk;
protected void
Page_Load(object sender, EventArgs e)
{
if
(!Page.IsPostBack)
{
SqlDataAdapter
da = new SqlDataAdapter("select*from Customer_Orders", cn);
DataTable
dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void
Button1_Click(object sender, EventArgs e)
{
int j =
GridView1.Rows.Count;
for (int i = 0; i < j; i++)
{
CheckBox
chk1;
chk1 = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox2");
if
(chk1.Checked == true)
{
string
s = chk1.Text;
int
i1 = int.Parse(s);
SqlConnection
con = new SqlConnection(@"Data Source=dfin25;Initial Catalog=pankaj
pandey;Integrated Security=true");
SqlCommand
cmd = new SqlCommand("delete from emp where EmpId=" + i1 + "", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
protected void
CheckBox2_CheckedChanged(object sender, EventArgs e)
{
bool
flg = true;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox
chk;
chk = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox2");
if
(chk.Checked == false)
{
flg = false;
break;
}
}
CheckBox1.Checked = flg;
}
protected void
CheckBox3_CheckedChanged(object sender, EventArgs e)
{
CheckBox
chk1;
chk1 = (CheckBox)GridView1.HeaderRow.FindControl("CheckBox3");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox
chk;
chk = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox2");
chk.Checked = chk1.Checked;
}
}
}