–JavaScript–

<script language=”javascript” type=”text/javascript”>

//user for select all checkbox
function SelectAllCheckboxes(spanChk){

// Added as ASPX uses SPAN for checkbox

var oItem = spanChk.children;
var theBox= (spanChk.type==”checkbox”) ?
spanChk : spanChk.children.item[0];
xState=theBox.checked;
elm=theBox.form.elements;

for(i=0;i<elm.length;i++)
if(elm[i].type==”checkbox” &&
elm[i].id!=theBox.id)
{
//elm[i].click();

if(elm[i].checked!=xState)
elm[i].click();
//elm[i].checked=xState;

}
}

//User for check any checkbox is selected or not and delete confirm message
function checkDelete(spanChk)
{
var chks=document.getElementsByTagName(‘input’);
var hasChecked = false;
for (var i = 0; i < chks.length; i++)
{
if (chks[i].checked)
{
hasChecked = true;
break;
}
}
if (hasChecked == false)
{
alert(“Please select at least one checkbox..!”);
return false;
}
else
{
if(confirm(“r u sure delete records?”))
{
return true;
}
else
{
return false;
}
}
}
</script>

–GridView .aspx page–

<asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”False” OnRowDeleting=”GridView1_RowDeleting” ShowFooter=”True”>
<Columns>
<asp:TemplateField HeaderText=”Select”>
<HeaderTemplate>
<input id=”chkAll” type=”checkbox” runat=”Server” onclick=”javascript:SelectAllCheckboxes(this);” />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID=”checkbox1″ runat=”Server” />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”Rollno”>
<ItemTemplate>
<asp:Label ID=”lblRollno” runat=”server” Text='<%# Eval(“rollno”) %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”Name”>
<ItemTemplate>
<asp:Label ID=”lblName” runat=”server” Text='<%# Eval(“name”) %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”City”>
<ItemTemplate>
<asp:Label ID=”lblCity” runat=”server” Text='<%# Eval(“city”) %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID=”lbnDelete” CommandName=”Delete” runat=”server” Text=”Delete”
OnClientClick=”javascript:return checkDelete(this);”></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterTemplate>
<asp:Button ID=”btnSubmit” runat=”server” Text=”Delete Selected” OnClick=”btnSubmit_Click”
OnClientClick=”javascript:return checkDelete(this);”  />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

—aspx.cs page—

Single Record Delete Coding From Gridview

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Label rno = (Label)GridView1.Rows[e.RowIndex].FindControl(“lblRollno”);
int no = Convert.ToInt32(rno.Text);

string str = “delete from tybca where rollno=” + no;
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[“TESTConnectionString”].ToString());
conn.Open();
SqlCommand cmd = new SqlCommand(str, conn);
cmd.ExecuteNonQuery();
BindGrid();
}

Multiple Selected Checkbox Record Deleteing Coding

protected void btnSubmit_Click(object sender, EventArgs e)
{
ArrayList chkList = new ArrayList();
Label rollno;
int rno;

//Find Multiple checkbox was checked

for(int row=0; row<GridView1.Rows.Count;row++)
{
CheckBox c = (CheckBox)GridView1.Rows[row].Cells[0].FindControl(“CheckBox1”);
if (c.Checked != null)
{
if (c.Checked)
{
rollno = (Label) GridView1.Rows[row].Cells[1].FindControl(“lblRollno”);
rno = Convert.ToInt32(rollno.Text);
chkList.Add(rno);
}
}
}

//Delete Multiple Records
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[“TESTConnectionString”].ToString());
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
string strSql;
foreach (int i in chkList)
{
//Response.Write(i);
strSql = “Delete from tybca where rollno=” + i;
cmd.CommandType = CommandType.Text;
cmd.CommandText = strSql;
cmd.ExecuteNonQuery();
}
BindGrid(); //this function bind gridview with records from datasource
}