How to delete multiple rows in Gridview?


You can do this like.

step1. Include the checkbox field in header of Gridview and write the JavaScript code to select the multiple rows of Gridview like this

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

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “”&gt;

<html xmlns=””&gt;
<head runat=”server”>

<script type = “text/javascript”>

function checkAll(objRef)

var GridView = objRef.parentNode.parentNode.parentNode;

var inputList = GridView.getElementsByTagName(“input”);

for (var i = 0; i < inputList.length; i++)

//Get the Cell To find out ColumnIndex

var row = inputList[i].parentNode.parentNode;

if (inputList[i].type == “checkbox” && objRef != inputList[i])

if (objRef.checked)
{ = “Silver”;
inputList[i].checked = true;


inputList[i].checked = false; = “white”;






<form id=”form1″ runat=”server”>

<div style=”margin-left:200px;”>

<asp:GridView ID=”GridView1″ runat=”server”
<asp:TemplateField HeaderText=”Select”>


<asp:CheckBox ID=”chkAll” runat=”server”

onclick = “checkAll(this);” />

<asp:CheckBox ID=”chkBox1″  runat=”server” />
<asp:Label ID=”lblId” runat=”server” Text='<%# Eval(“Id”) %>’ Visible=”false” />


<asp:TemplateField HeaderText=”SI.No”>
<%#Container.DataItemIndex+1 %>

<asp:BoundField HeaderText=”EmpName” DataField=”EmpName” />
<asp:BoundField HeaderText=”EmpSal” DataField=”EmpSal” />


<asp:Button ID=”BtnDelete” runat=”server” Text=”Delete”
CausesValidation=”false” OnClientClick=”return confirm(‘Are you sure?’)”
onclick=”BtnDelete_Click” />



Step2: Write the code for checking selected row of gridview and take the Id of that row.

if  check box checked is true then delete that row of gridview

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)

protected void FillGrid()
using (SqlConnection con = new SqlConnection(“Data Source=.;Initial Catalog=Test;Integrated Security=True”))
using (SqlCommand cmd = new SqlCommand(“Select top 10 *from tblEmp”, con))
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
GridView1.DataSource = dt;
protected void BtnDelete_Click(object sender, EventArgs e)
foreach (GridViewRow row in GridView1.Rows)
CheckBox cb = (CheckBox)row.FindControl(“chkBox1”);
Label lbl=(Label)row.FindControl(“lblId”);
string uid =lbl.Text ;
if (cb.Checked)
using( SqlConnection con = new SqlConnection(“Data Source=.;Initial Catalog=Test;Integrated Security=True”))

using (SqlCommand cmd = new SqlCommand(“Delete from tblEmp where Id=@Id”, con))

cmd.Parameters.AddWithValue(“@Id”, uid);




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.