Asp.Net |
C#.Net |
Gridview Edit & delete |
Download The Source Here :
GridView Editing
Here with i composed Gridview Editing and deleting in Asp.Net using C#.Net
DATABASE & TABLE :
Create The Database With the Name Gv & In That Execute The Below Query
GO
/****** Object: Table [dbo].[Registration] Script Date: 03/27/2012 23:30:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Registration](
[UID] [bigint] IDENTITY(1,1) NOT NULL,
[UserName] [nvarchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[PassWord] [nvarchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[ConfirmPassWord] [nvarchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Address] [nvarchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Age] [bigint] NULL,
[BloodGroup] [nvarchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[PhoneNo] [bigint] NULL,
[Email] [nvarchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[DOB] [datetime] NULL,
CONSTRAINT [PK_Registration] PRIMARY KEY CLUSTERED
(
[UID] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
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">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Data Grid</title>
</head>
<body>
<form id="form1" runat="server">
<%--<div allowpaging="true" style="height: 180px">--%>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
CellPadding="3" CellSpacing="2" Height="16px"
onpageindexchanging="GridView1_PageIndexChanging"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating" Width="178px"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<Columns>
<asp:TemplateField HeaderText="UID">
<ItemTemplate>
<asp:Label ID = "lbl1" runat = "server" Text = '<% #bind("UID") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label id = "lbl1" runat = "server" text = '<% #bind("UID") %>'>
</asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UserName">
<ItemTemplate>
<asp:Label ID = "lbl2" runat = "server" Text = '<% #bind("UserName") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id = "txtusername" runat = "server" text = '<% #bind("UserName") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PassWord">
<ItemTemplate>
<asp:Label ID = "lbl3" runat = "server" Text = '<% #bind("PassWord") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id = "txtpassword" runat = "server" text = '<% #bind("PassWord") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ConfirmPassWord">
<ItemTemplate>
<asp:Label ID = "lbl4" runat = "server" Text = '<% #bind("ConfirmPassWord") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id = "txtconfirmpassword" runat = "server" text = '<% #bind("ConfirmPassWord") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:Label ID = "lbl5" runat = "server" Text = '<% #bind("Address") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id = "txtaddress" runat = "server" text = '<% #bind("Address") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age">
<ItemTemplate>
<asp:Label ID = "lbl6" runat = "server" Text = '<% #bind("Age") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id = "txtage" runat = "server" text = '<% #bind("Age") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="BloodGroup">
<ItemTemplate>
<asp:Label ID = "lbl7" runat = "server" Text = '<% #bind("BloodGroup") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id = "txtbloodgroup" runat = "server" text = '<% #bind("BloodGroup") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PhoneNo">
<ItemTemplate>
<asp:Label ID = "lbl8" runat = "server" Text = '<% #bind("PhoneNo") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id = "txtphoneno" runat = "server" text = '<% #bind("PhoneNo") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label ID = "lbl9" runat = "server" Text = '<% #bind("Email") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id = "txtemail" runat = "server" text = '<% #bind("Email") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DOB">
<ItemTemplate>
<asp:Label ID = "lbl10" runat = "server" Text = '<% #bind("DOB") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id = "txtdob" runat = "server" text = '<% #bind("DOB") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate >
<asp:LinkButton ID ="lnk1" runat ="server" Text ="Edit" CommandName ="Edit">
</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID ="lnk2" runat ="server" Text ="Update" CommandName ="Update">
</asp:LinkButton>
<asp:LinkButton ID ="lnk3" runat ="server" Text ="Cancel" CommandName ="Cancel">
</asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate >
<asp:LinkButton ID ="lnk4" runat="server" Text ="Delete" CommandName ="delete">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
</asp:GridView>
<%--</div>--%>
<br />
</form>
</body>
</html>
Default.aspx.cs :
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Collections;
using System.Net;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("data source=GV\\SQLEXPRESS;initial catalog = gv;user id=sa; password=sqldata; ");
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
//int index = 0;
protected void Display()
{
con.Open();
cmd.Connection = con;
da = new SqlDataAdapter("select * from registration", con);
da.Fill(ds, "registration");
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
Display();
}
}
catch (Exception ex)
{
throw ex;
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
GridView1.PageIndex = e.NewPageIndex;
Display();
}
catch(Exception ex)
{
throw ex;
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
GridViewRow r = GridView1.Rows[e.RowIndex];
Label idlbl;
idlbl = (Label)r.FindControl("lbl1");
con.Open();
cmd.Connection = con;
cmd.CommandText = "delete from registration where uid = '" + idlbl.Text + "'";
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
Display();
}
catch(Exception ex)
{
throw ex;
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
GridViewRow r = GridView1.Rows[e.RowIndex];
TextBox UN, PWd, CPWd, Add,Age,BG,PN,Eml,Dt;
Label id;
UN = (TextBox)r.FindControl("txtUserName");
PWd = (TextBox)r.FindControl("txtPassWord");
CPWd = (TextBox)r.FindControl("txtConfirmPassWord");
Add = (TextBox)r.FindControl("txtAddress");
Age = (TextBox)r.FindControl("txtAge");
BG = (TextBox)r.FindControl("txtBloodGroup");
PN = (TextBox)r.FindControl("txtPhoneNo");
Eml = (TextBox)r.FindControl("txtemail");
Dt = (TextBox)r.FindControl("txtdob");
id = (Label)r.FindControl("lbl1");
con.Open();
cmd.Connection = con;
cmd.CommandText = "update registration set UserName = '" + UN.Text + "',PassWord = '" + PWd.Text + "',ConfirmPassWord = '" + CPWd.Text + "',Address = '" + Add.Text + "',Age = '" + Age.Text + "',BloodGroup = '" + BG.Text + "',PhoneNo = '" + PN.Text + "',email = '" + Eml.Text + "',dob = '" + Dt.Text + "' where uid = '" + id.Text + "' ";
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
Display();
}
catch (Exception ex)
{
throw ex;
}
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
try
{
GridView1.EditIndex = -1;
Display();
}
catch (Exception ex)
{
throw ex;
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
try
{
GridView1.EditIndex = e.NewEditIndex;
Display();
}
catch (Exception ex)
{
throw ex;
}
}
}
0 comments:
Post a Comment