Delete data from gridview in asp.net c#
Database:-
Source code:-
<%@ Page
Language="C#"
AutoEventWireup="true"
CodeFile="gridview.aspx.cs"
Inherits="gridview"
%>
<!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: 100%;
}
</style>
<script type="text/javascript">
function ConfirmationBox(username) {
var result = confirm('Are
you sure you want to delete ' + username + '
Details');
if (result) {
return true;
}
else {
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td
align="center">
<asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="False"
DataKeyNames="ID" BackColor="White"
BorderColor="#CCCCCC"
BorderStyle="None" BorderWidth="1px"
CellPadding="3"
>
<Columns>
<asp:BoundField DataField="ID"
HeaderText="ID"
/>
<asp:BoundField DataField="Name"
HeaderText="Name"
/>
<asp:BoundField DataField="Phone"
HeaderText="Phone
no" />
<asp:TemplateField HeaderText="Delete User
Information">
<ItemTemplate>
<asp:LinkButton ID="lnkdelete"
runat="server"
OnClick="lnkdelete_Click">Delete
User</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White"
ForeColor="#000066"
/>
<HeaderStyle BackColor="#006699"
Font-Bold="True"
ForeColor="White"
/>
<PagerStyle BackColor="White"
ForeColor="#000066"
HorizontalAlign="Left"
/>
<RowStyle ForeColor="#000066"
/>
<SelectedRowStyle BackColor="#669999"
Font-Bold="True"
ForeColor="White"
/>
<SortedAscendingCellStyle BackColor="#F1F1F1"
/>
<SortedAscendingHeaderStyle BackColor="#007DBB"
/>
<SortedDescendingCellStyle BackColor="#CAC9C9"
/>
<SortedDescendingHeaderStyle BackColor="#00547E"
/>
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Design:-
C#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.SqlClient;
using System.Data;
using System.Configuration;
public partial class gridview :
System.Web.UI.Page
{
String ConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
protected void
Page_Load(object sender, EventArgs e)
{
SqlConnection con = new
SqlConnection(ConnString);
if (!IsPostBack)
{
BindUserDetails();
}
}
protected void
BindUserDetails()
{
SqlConnection con = new
SqlConnection(ConnString);
//connection open
con.Open();
//sql command to execute query from database
SqlCommand
cmd = new SqlCommand("Select * from reg", con);
cmd.ExecuteNonQuery();
SqlDataAdapter da = new
SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
//Binding data to gridview
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
protected void
GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void
GridView1_DataBound(object sender, EventArgs e)
{
}
protected void
GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//getting username from particular row
string username = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Name"));
//identifying the control in gridview
LinkButton lnkbtnresult = (LinkButton)e.Row.FindControl("lnkdelete");
//raising javascript confirmationbox whenver user clicks on
link button
lnkbtnresult.Attributes.Add("onclick",
"javascript:return ConfirmationBox('"
+ username + "')");
}
}
protected void
lnkdelete_Click(object sender, EventArgs e)
{
SqlConnection con = new
SqlConnection(ConnString);
LinkButton lnkbtn = sender as LinkButton;
//getting particular row linkbutton
GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
//getting userid of particular row
int ID = Convert.ToInt32(GridView1.DataKeys[gvrow.RowIndex].Value.ToString());
string Name = gvrow.Cells[1].Text;
con.Open();
SqlCommand cmd = new
SqlCommand("delete
from reg where ID =" + ID, con);
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
BindUserDetails();
//Displaying alert message after successfully deletion of
user
ScriptManager.RegisterStartupScript(this, this.GetType(),
"alertmessage", "javascript:alert('" + Name + " details deleted successfully')", true);
}
}
}
Delete data from gridview in asp.net c#
Reviewed by NEERAJ SRIVASTAVA
on
5:24:00 PM
Rating:
No comments: