code behimd: c#
using System;
using System.Collections;
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;
public partial class Gridviewevents : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
#region BIND
public void bind()
{
string strevent = ConfigurationManager.AppSettings["constr"].ToString();
SqlConnection ocon = new SqlConnection(strevent);
ocon.Open();
string sql = "SELECT * FROM GRIDEVENT";
SqlDataAdapter odsa = new SqlDataAdapter(sql, ocon);
DataSet ds = new DataSet();
odsa.Fill(ds, "temp");
Gridevent.DataSource = ds.Tables[0].DefaultView;
Gridevent.DataBind();
ocon.Close();
}
#endregion
#region ROW UPDATING
protected void Gridevent_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string strevent = ConfigurationManager.AppSettings["constr"].ToString();
SqlConnection ocon = new SqlConnection(strevent);
GridViewRow row = (GridViewRow)Gridevent.Rows[e.RowIndex];
Label lblsid = (Label)row.FindControl("lblid");
TextBox txtname = (TextBox)row.FindControl("txtname");
TextBox txtadd = (TextBox)row.FindControl("txtadd");
Gridevent.EditIndex = -1;
ocon.Open();
SqlCommand cmd = new SqlCommand("update gridevent set NAME='" + txtname.Text + "',ADRESS='" + txtadd.Text + "' where sid=" + lblsid.Text + "", ocon);
cmd.ExecuteNonQuery();
ocon.Close();
bind();
}
#endregion
#region ROW EDITING
protected void Gridevent_RowEditing(object sender, GridViewEditEventArgs e)
{
Gridevent.EditIndex = e.NewEditIndex;
bind();
}
#endregion
#region ROWDELETING
protected void Gridevent_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string strevent = ConfigurationManager.AppSettings["constr"].ToString();
SqlConnection ocon = new SqlConnection(strevent);
GridViewRow row = (GridViewRow)Gridevent.Rows[e.RowIndex];
Label lblsid = (Label)row.FindControl("lblid");
ocon.Open();
SqlCommand cmd = new SqlCommand("delete gridevent where sid=" + lblsid.Text + "", ocon);
cmd.ExecuteNonQuery();
ocon.Close();
bind();
}
#endregion
#region ROWCACELING
protected void Gridevent_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
Gridevent.EditIndex = -1;
bind();
}
#endregion
#region PAGEINDEXCHANGING
protected void Gridevent_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
Gridevent.PageIndex = e.NewPageIndex;
bind();
}
#endregion
}
Asp.net design:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Gridviewevents.aspx.cs" Inherits="Gridviewevents" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="Gridevent" runat="server" AutoGenerateColumns ="false"
AutoGenerateEditButton ="true" AutoGenerateDeleteButton ="true"
onrowcancelingedit="Gridevent_RowCancelingEdit"
onrowdeleting="Gridevent_RowDeleting" onrowediting="Gridevent_RowEditing"
onrowupdating="Gridevent_RowUpdating"
onpageindexchanging="Gridevent_PageIndexChanging" >
<Columns >
<asp:TemplateField HeaderText ="SID">
<ItemTemplate >
<asp:Label ID="lblid" runat ="server" Text ='<%#Eval("SID")%>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="NAME">
<ItemTemplate >
<asp:Label ID="lblname" runat ="server" Text ='<%#Eval("NAME")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID ="txtname" runat ="server" Text ='<%#Eval("NAME")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="ADDRESS">
<ItemTemplate >
<asp:Label ID="lbladd" runat ="server" Text ='<%#Eval("ADRESS")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID ="txtadd" runat ="server" Text ='<%#Eval("ADRESS")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
No comments:
Post a Comment