Multiple Row Deletion From GRIDVIEW

Share on TwitterShare on TumblrSubmit to StumbleUponSave on DeliciousDigg This

Hi in this article iam going to show how to delete multiple rows in a gridview. In this i made more simpler by accessing SQL DataSource .

 

 

DELETE MULTIPLE RECORDS IN GRIDVIEW

 

Introduction :

Generally using a GridView we can delete only a single row but in this article I’m going explain how to delete multiple rows using a single button click in an easiest way. I use Sql data source to make my job simpler.

 

STEP 1 : Create a Web Application from Visual Studio .
STEP 2 : Copy the code given below as Default.aspx
STEP 2 : The Default.aspx page is included with the Gridview, Button and the SqlDataSource
STEP 3 : Now copy Default.aspx.cs code.
STEP 4 : That’s it now run and enjoy the gridview with multiple delete.

 

SCREENSHOTS :

First the Default.aspx looks like this  :

 

 

 

 

 

 

Here now a user can check the records he wanted to delete and click the Delete Rows Button

 

 

 

 

 

 

Now a popup window arrises , actually here in my example I checked records with serial number 3,4,5 so now if select ok in that prompt window ,all the 3 records will be deleted .

That can be seen this way :

 

 

 

 

 

 

 

CODE :

My code part comprises of 3 files they are

Default.aspx

<%@ 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" language="javascript">
function DeleteConfirmation()
{
if (confirm("Are you sure you want to delete selected records ?")==true)
return true;
else
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div><center><br /><h1>MULTIPLE ROW DELETE</h1><br /><br /><asp:GridView BackColor="Azure" AlternatingRowStyle-BackColor="AliceBlue" ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID"DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField HeaderStyle-BackColor="Orange" HeaderText="CHECK TO DELETE">
<ItemTemplate>
<asp:CheckBox ID="chkRows" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderStyle-BackColor="Orange" DataField="ProductID" HeaderText="ProductID" ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField HeaderStyle-BackColor="Orange" DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" /><asp:BoundField HeaderStyle-BackColor="Orange" DataField="UnitPrice" HeaderText="UnitPrice" SortExpression="UnitPrice" />
<asp:BoundField HeaderStyle-BackColor="Orange" DataField="Rating" HeaderText="Rating" SortExpression="Rating" />
</Columns>
</asp:GridView>
</center>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:srikanthchConnectionString %>"
SelectCommand="SELECT * FROM [muldel]"
DeleteCommand="DELETE FROM muldel WHERE [ProductID] = @ProductID">
<DeleteParameters>
<asp:Parameter Name="ProductID" />
</DeleteParameters>
</asp:SqlDataSource><br /><br />
<center> <asp:Button 
ID="btnMultipleRowDelete" BackColor="Orange"
OnClick="btnMultipleRowDelete_Click" OnClientClick="return DeleteConfirmation();"
runat="server"
Text="Delete Rows" /></center>
</form>
</body>
</html>

 

 Now the Default.aspx.cs

 

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)
{
}
protected void btnMultipleRowDelete_Click(object sender, EventArgs e)
{
// Looping through all the rows in the GridView
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox checkbox = (CheckBox)row.FindControl("chkRows");
//Check if the checkbox is checked.
//value in the HtmlInputCheckBox's Value property is set as the 
//value of the delete command's parameter.
if (checkbox.Checked)
{
// Retreive the Product ID
int productID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
// Pass the value of the selected Product ID to the Delete
SqlDataSource1.DeleteParameters["ProductID"].DefaultValue = productID.ToString();
SqlDataSource1.Delete();
}
}
}
}

 

 Now the Web.Config file ( For Connection String)

 

 <?xml version="1.0"?>

<configuration>

<appSettings/>

<connectionStrings>

<add name="srikanthchConnectionString" connectionString="Data Source=USER-182;Initial Catalog=srikanthch;Persist Security Info=True;User ID=sa;Password=007" providerName="System.Data.SqlClient"/>

</connectionStrings>

<system.web>

<compilation debug="true" targetFramework="4.0"/>

<authentication mode="Windows"/>

<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/></system.web>

</configuration>

 

Conclusion :

By this way we could able to delete multiple rows in a GridView using a SqlDataSource.

prajapat