Delete Multiple Rows from Gridview
The code below is to delete multiple rows form gridview at once. In this example DataTable is used to bind GridView.
-----------------------------------------------------------------------------------------------------------------------------------
ASPX Page Code
-----------------------------------------------------------------------------------------------------------------------------------
<%@ 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></title>
<style type="text/css">
.style1
{
width: 50%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<span style="width:50%; margin:0 auto; display:block; color:Green; font-family:Arial; font-size:13px; letter-spacing:1px; line-height:1.7em; padding-top:20px; padding-bottom:30px;">
Example to delete meultiple rows from gridview at once. First add values in text box and click add.
Add as many values as you want. Check the checkboxes for rows you want to delete and click Delete.
In this example DataTable is used to bing GridView.
</span>
<table align="center" class="style1">
<tr>
<td style="text-align: right">
Name</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td style="text-align: right">
Age</td>
<td>
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnAdd" runat="server" Text="Add" Width="100px"
onclick="btnAdd_Click" />
</td>
</tr>
</table>
<br />
<center>
<asp:GridView ID="gridAdd" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None">
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<Columns>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:CheckBox ID="chkDelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age">
<ItemTemplate>
<asp:TextBox ID="txtAge" runat="server" Text='<%# Bind("Age") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<br />
<asp:Button ID="btnDelete" runat="server" onclick="btnDelete_Click"
Text="Delete" Width="100px" />
</center>
</div>
</form>
</body>
</html>
-----------------------------------------------------------------------------------------------------------------------------------
ASPX Page Code
-----------------------------------------------------------------------------------------------------------------------------------
<%@ 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></title>
<style type="text/css">
.style1
{
width: 50%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<span style="width:50%; margin:0 auto; display:block; color:Green; font-family:Arial; font-size:13px; letter-spacing:1px; line-height:1.7em; padding-top:20px; padding-bottom:30px;">
Example to delete meultiple rows from gridview at once. First add values in text box and click add.
Add as many values as you want. Check the checkboxes for rows you want to delete and click Delete.
In this example DataTable is used to bing GridView.
</span>
<table align="center" class="style1">
<tr>
<td style="text-align: right">
Name</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td style="text-align: right">
Age</td>
<td>
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnAdd" runat="server" Text="Add" Width="100px"
onclick="btnAdd_Click" />
</td>
</tr>
</table>
<br />
<center>
<asp:GridView ID="gridAdd" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None">
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<Columns>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:CheckBox ID="chkDelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age">
<ItemTemplate>
<asp:TextBox ID="txtAge" runat="server" Text='<%# Bind("Age") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<br />
<asp:Button ID="btnDelete" runat="server" onclick="btnDelete_Click"
Text="Delete" Width="100px" />
</center>
</div>
</form>
</body>
</html>
-----------------------------------------------------------------------------------------------------------------------------------
CS Page Code
-----------------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
static DataTable dtGrid; // Declare Static Datatable
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DummyDataTable();
btnDelete.Visible = false;
}
}
#region Dummy DataTable
public void DummyDataTable() // Add columns in datatable to create layout of table
{
dtGrid = new DataTable("Details");
DataColumn dcName = new DataColumn("Name");
DataColumn dcAge = new DataColumn("Age");
DataColumn check = new DataColumn("Check"); // this column holds the value of checkbox
dtGrid.Columns.Add(dcName);
dtGrid.Columns.Add(dcAge);
dtGrid.Columns.Add(check);
gridAdd.DataSource = dtGrid;
gridAdd.DataBind();
}
#endregion
#region Add Values to Gridview
protected void btnAdd_Click(object sender, EventArgs e) // Adding values to datatable
{
DataRow drGrid = dtGrid.NewRow();
drGrid["Name"] = txtName.Text;
drGrid["Age"] = txtAge.Text;
drGrid["Check"] = "0"; // setting 0 as default for each row
dtGrid.Rows.Add(drGrid);
gridAdd.DataSource = dtGrid;
gridAdd.DataBind();
btnDelete.Visible = true;
}
#endregion
#region Delete Multipple Rows From Grid View
protected void btnDelete_Click(object sender, EventArgs e)
{
int chk = 0;
for (int i = 0; i < gridAdd.Rows.Count; i++)
{
if (((CheckBox)(gridAdd.Rows[i].FindControl("chkDelete"))).Checked)
{
dtGrid.Rows[i]["Check"] = "1"; // if checkbox is checked then set value to 1
chk = 1;
}
}
if (chk > 0)
{
for (int j = 0; j < dtGrid.Rows.Count; j++)
{
if (dtGrid.Rows[j]["Check"] == "1")
{
dtGrid.Rows[j].Delete(); // Delete those rows form datatable where Check value = 1
j = -1;
}
}
gridAdd.DataSource = dtGrid; // Bind gridview to datatable
gridAdd.DataBind();
if (dtGrid.Rows.Count > 0)
{
btnDelete.Visible = true;
}
else
{
btnDelete.Visible = false;
}
}
else
{
Response.Write("<script language='javascript'>alert('No row checked.\\r\\nPlease check row to delete form gridview.');</script>");
}
}
#endregion
}
Download complete code from the link
http://www.navvtech.com/demo/dotnet/GridViewMultiDelete.rar
Comments
Post a Comment