How to delete multiple rows in Gridview?


Hi

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” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;

<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head runat=”server”>
<title></title>

<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)
{

row.style.backgroundColor = “Silver”;
inputList[i].checked = true;

}

else
{
inputList[i].checked = false;
row.style.backgroundColor = “white”;

}

}

}

}

</script>

</head>
<body>
<form id=”form1″ runat=”server”>

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

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

<HeaderTemplate>

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

onclick = “checkAll(this);” />

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

</ItemTemplate>
</asp:TemplateField>

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

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

</asp:GridView>

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

</div>

</form>
</body>
</html>

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)
{
FillGrid();
}

}
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();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
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);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
FillGrid();
}

}

}
}
}
}

Advertisement

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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.