How to place delete all button inside the gridview in header in asp.net c#
In this article, I will show you that how we can
delete the particular row in the grid view. We will add the link button inside the header the
grid view and we will create the click event of the link button for delete operation .
Sql Server (Data Base):-
Table Design:-
Script the creation of table:-
USE [CodeSolution]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[reg](
[id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[Gender] [nvarchar](50) NULL,
[Email] [nvarchar](50) NULL,
CONSTRAINT
[PK_reg] PRIMARY KEY
CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS
= ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
Data in table:-
Design:-
Source Code:-
<%@
Page Language="C#" AutoEventWireup="true" CodeFile="deleteingridview.aspx.cs"
Inherits="deleteingridview" %>
<!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>delete all
button inside the gridview</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvdetails" runat="server" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="ID" Width="560px" GridLines="None" Font-Names="Times New Roman"
ForeColor="#333333" RowStyle-HorizontalAlign="Center">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:LinkButton ID="lnkdelete" runat="server"
ItemStyle-Width="90px"
OnClick="lnkdelete_Click"
Text="Delete
All"
Style="color: White; text-decoration:
none;"></asp:LinkButton>
</HeaderTemplate>
</asp:TemplateField>
<asp:BoundField DataField="name" HeaderText="Name" SortExpression="Name" ItemStyle-Width="90px" />
<asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender" ItemStyle-Width="90px" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" ItemStyle-Width="90px" />
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" ForeColor="White" Font-Bold="True" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="White" HorizontalAlign="Center" BackColor="#666666" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
</div>
</form>
</body>
</html>
Code behind(c#):-
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data.SqlClient;
using System.Data;
public partial class deleteingridview : System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{
BindUserDetails();
}
protected void lnkdelete_Click(object
sender, EventArgs e)
{
using (SqlConnection con = new
SqlConnection("Data
Source=NEERAJ-PC;Initial Catalog=CodeSolution;Persist Security Info=True;User
ID=sa; password=12345678"))
{
using
(SqlCommand cmd = new
SqlCommand("",
con))
{
cmd.CommandText = "delete from
reg";
cmd.Connection = con;
con.Open();
SqlDataReader
dr = cmd.ExecuteReader();
DataTable
dataTable = new DataTable();
dataTable.Load(dr);
gvdetails.DataSource =
dataTable;
gvdetails.DataBind();
Response.Write("<script>alert('Record successfully
delete')</script>");
BindUserDetails();
}
}
}
protected void BindUserDetails()
{
SqlConnection
con = new SqlConnection("Data Source=NEERAJ-PC;Initial
Catalog=CodeSolution;Persist Security Info=True;User ID=sa;
password=12345678");
SqlCommand
cmd = new SqlCommand(("select * from reg"), con);
con.Open();
SqlDataAdapter
da = new SqlDataAdapter(cmd);
DataSet
ds = new DataSet();
da.Fill(ds);
con.Close();
if
(ds.Tables[0].Rows.Count > 0)
{
gvdetails.DataSource = ds;
gvdetails.DataBind();
}
else
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
gvdetails.DataSource = ds;
gvdetails.DataBind();
int
columncount = gvdetails.Rows[0].Cells.Count;
gvdetails.Rows[0].Cells.Clear();
gvdetails.Rows[0].Cells.Add(new TableCell());
gvdetails.Rows[0].Cells[0].ColumnSpan = columncount;
gvdetails.Rows[0].Cells[0].Text = "No Records Found";
}
}
}
Out-Put:-
How to place delete all button inside the gridview in header in asp.net c#
Reviewed by NEERAJ SRIVASTAVA
on
12:09:00 PM
Rating:
you are deleting record of all table but i want to delete,update,insert record on popup button which i want,,,,i want also to add images in datatable,,,,w8ng for ur reply
ReplyDeleteDear Ali , First In this article , i am deleting the all record of one table (reg) and second that you want , I do not have exactly code or article but below link , it will be help for you
Deletehttp://neerajcodesolution.blogspot.in/2013/06/edit-insertdeleteupdate-in-gridview-in.html