design the page :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
<div>
<asp:UpdatePanel ID="up" runat="server">
<ContentTemplate>
<table align="center">
<tr><td> </td><td><asp:Label ID="lb" runat="server"></asp:Label></td></tr>
<tr><td>EID</td><td><asp:TextBox ID="tb" runat="server"></asp:TextBox></td></tr>
<tr><td>NAME</td><td><asp:TextBox ID="tb1" runat="server"></asp:TextBox></td></tr>
<tr><td>SALARY</td><td><asp:TextBox ID="tb2" runat="server"></asp:TextBox></td></tr>
<tr><td> </td><td><asp:Button ID="btn" runat="server" Text="Save" Width="80"
onclick="btn_Click" /> <asp:Button ID="Button1" runat="server" Text="Reset" Width="80" /></td></tr>
<tr><td> </td><td>
<asp:GridView DataKeyNames="eid"
EmptyDataText="Records not avalible." Width="100%" ID="gv" runat="server" AllowPaging="True"
AllowSorting="True" PageSize="2"
onpageindexchanging="gv_PageIndexChanging" onsorting="gv_Sorting"
onrowcancelingedit="gv_RowCancelingEdit" onrowdeleting="gv_RowDeleting"
onrowediting="gv_RowEditing" onrowupdating="gv_RowUpdating"
onrowdatabound="gv_RowDataBound" ShowFooter="True" GridLines="Horizontal">
<Columns>
<asp:CommandField HeaderText="UPDATE" ShowEditButton="True" ShowHeader="True" />
<asp:CommandField HeaderText="DELETE" ShowDeleteButton="True"
ShowHeader="True" />
<asp:TemplateField HeaderText="Sl No.">
<ItemTemplate>
<%#Container.DisplayIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView></td></tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
The page looks like :
C# code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseModel;
public partial class Default5 : System.Web.UI.Page
{
DatabaseEntities1 o = new DatabaseEntities1();
int i,j=0,k=0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
view();
ViewState["a"] = 1;
}
}
void view()
{
gv.DataSource = o.EMPs;
gv.DataBind();
}
protected void btn_Click(object sender, EventArgs e)
{
EMP p = new EMP();
p.EID = Convert.ToInt32(tb.Text);
p.NAME = tb1.Text;
p.SAL = Convert.ToInt32(tb2.Text);
o.EMPs.AddObject(p);
o.SaveChanges();
view();
lb.Text = "One record saved.";
lb.ForeColor = System.Drawing.Color.Green;
}
protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gv.PageIndex = e.NewPageIndex;
view();
}
protected void gv_Sorting(object sender, GridViewSortEventArgs e)
{
if (e.SortExpression.ToString() == "eid")
{
if (Convert.ToInt32(ViewState["a"]) == 1)
{
var m = from x in o.EMPs orderby x.EID descending select x;
gv.DataSource = m;
gv.DataBind();
ViewState["a"] = 2;
}
else
{
var m = from x in o.EMPs orderby x.EID ascending select x;
gv.DataSource = m;
gv.DataBind();
ViewState["a"] = 1;
}
}
else if (e.SortExpression.ToString() == "sal")
{
if (Convert.ToInt32(ViewState["a"]) == 1)
{
var m = from x in o.EMPs orderby x.SAL descending select x;
gv.DataSource = m;
gv.DataBind();
ViewState["a"] = 2;
}
else
{
var m = from x in o.EMPs orderby x.SAL ascending select x;
gv.DataSource = m;
gv.DataBind();
ViewState["a"] = 1;
}
}
else
{
if (Convert.ToInt32(ViewState["a"]) == 1)
{
var m = from x in o.EMPs orderby x.NAME descending select x;
gv.DataSource = m;
gv.DataBind();
ViewState["a"] = 2;
}
else
{
var m = from x in o.EMPs orderby x.NAME ascending select x;
gv.DataSource = m;
gv.DataBind();
ViewState["a"] = 1;
}
}
}
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
view();
}
protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
i=Convert.ToInt32(gv.DataKeys[e.RowIndex].Value);
var m = (from x in o.EMPs where x.EID ==i select x).First();
m.NAME = ((TextBox)gv.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
m.SAL = Convert.ToInt32(((TextBox)gv.Rows[e.RowIndex].Cells[5].Controls[0]).Text);
o.SaveChanges();
lb.Text = "One record updated.";
lb.ForeColor = System.Drawing.Color.Green;
gv.EditIndex = -1;
view();
}
protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gv.EditIndex = -1;
view();
}
protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
i = Convert.ToInt32(gv.DataKeys[e.RowIndex].Value);
var m = (from x in o.EMPs where x.EID == i select x).First();
o.EMPs.DeleteObject(m);
o.SaveChanges();
lb.Text = "One record deleted.";
lb.ForeColor = System.Drawing.Color.Green;
view();
}
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
j = j + Convert.ToInt32(e.Row.Cells[5].Text);
}
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[3].Text = "Total";
e.Row.Cells[5].Text = j.ToString();
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
<div>
<asp:UpdatePanel ID="up" runat="server">
<ContentTemplate>
<table align="center">
<tr><td> </td><td><asp:Label ID="lb" runat="server"></asp:Label></td></tr>
<tr><td>EID</td><td><asp:TextBox ID="tb" runat="server"></asp:TextBox></td></tr>
<tr><td>NAME</td><td><asp:TextBox ID="tb1" runat="server"></asp:TextBox></td></tr>
<tr><td>SALARY</td><td><asp:TextBox ID="tb2" runat="server"></asp:TextBox></td></tr>
<tr><td> </td><td><asp:Button ID="btn" runat="server" Text="Save" Width="80"
onclick="btn_Click" /> <asp:Button ID="Button1" runat="server" Text="Reset" Width="80" /></td></tr>
<tr><td> </td><td>
<asp:GridView DataKeyNames="eid"
EmptyDataText="Records not avalible." Width="100%" ID="gv" runat="server" AllowPaging="True"
AllowSorting="True" PageSize="2"
onpageindexchanging="gv_PageIndexChanging" onsorting="gv_Sorting"
onrowcancelingedit="gv_RowCancelingEdit" onrowdeleting="gv_RowDeleting"
onrowediting="gv_RowEditing" onrowupdating="gv_RowUpdating"
onrowdatabound="gv_RowDataBound" ShowFooter="True" GridLines="Horizontal">
<Columns>
<asp:CommandField HeaderText="UPDATE" ShowEditButton="True" ShowHeader="True" />
<asp:CommandField HeaderText="DELETE" ShowDeleteButton="True"
ShowHeader="True" />
<asp:TemplateField HeaderText="Sl No.">
<ItemTemplate>
<%#Container.DisplayIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView></td></tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
The page looks like :
C# code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseModel;
public partial class Default5 : System.Web.UI.Page
{
DatabaseEntities1 o = new DatabaseEntities1();
int i,j=0,k=0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
view();
ViewState["a"] = 1;
}
}
void view()
{
gv.DataSource = o.EMPs;
gv.DataBind();
}
protected void btn_Click(object sender, EventArgs e)
{
EMP p = new EMP();
p.EID = Convert.ToInt32(tb.Text);
p.NAME = tb1.Text;
p.SAL = Convert.ToInt32(tb2.Text);
o.EMPs.AddObject(p);
o.SaveChanges();
view();
lb.Text = "One record saved.";
lb.ForeColor = System.Drawing.Color.Green;
}
protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gv.PageIndex = e.NewPageIndex;
view();
}
protected void gv_Sorting(object sender, GridViewSortEventArgs e)
{
if (e.SortExpression.ToString() == "eid")
{
if (Convert.ToInt32(ViewState["a"]) == 1)
{
var m = from x in o.EMPs orderby x.EID descending select x;
gv.DataSource = m;
gv.DataBind();
ViewState["a"] = 2;
}
else
{
var m = from x in o.EMPs orderby x.EID ascending select x;
gv.DataSource = m;
gv.DataBind();
ViewState["a"] = 1;
}
}
else if (e.SortExpression.ToString() == "sal")
{
if (Convert.ToInt32(ViewState["a"]) == 1)
{
var m = from x in o.EMPs orderby x.SAL descending select x;
gv.DataSource = m;
gv.DataBind();
ViewState["a"] = 2;
}
else
{
var m = from x in o.EMPs orderby x.SAL ascending select x;
gv.DataSource = m;
gv.DataBind();
ViewState["a"] = 1;
}
}
else
{
if (Convert.ToInt32(ViewState["a"]) == 1)
{
var m = from x in o.EMPs orderby x.NAME descending select x;
gv.DataSource = m;
gv.DataBind();
ViewState["a"] = 2;
}
else
{
var m = from x in o.EMPs orderby x.NAME ascending select x;
gv.DataSource = m;
gv.DataBind();
ViewState["a"] = 1;
}
}
}
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
view();
}
protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
i=Convert.ToInt32(gv.DataKeys[e.RowIndex].Value);
var m = (from x in o.EMPs where x.EID ==i select x).First();
m.NAME = ((TextBox)gv.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
m.SAL = Convert.ToInt32(((TextBox)gv.Rows[e.RowIndex].Cells[5].Controls[0]).Text);
o.SaveChanges();
lb.Text = "One record updated.";
lb.ForeColor = System.Drawing.Color.Green;
gv.EditIndex = -1;
view();
}
protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gv.EditIndex = -1;
view();
}
protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
i = Convert.ToInt32(gv.DataKeys[e.RowIndex].Value);
var m = (from x in o.EMPs where x.EID == i select x).First();
o.EMPs.DeleteObject(m);
o.SaveChanges();
lb.Text = "One record deleted.";
lb.ForeColor = System.Drawing.Color.Green;
view();
}
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
j = j + Convert.ToInt32(e.Row.Cells[5].Text);
}
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[3].Text = "Total";
e.Row.Cells[5].Text = j.ToString();
}
}
}
No comments:
Post a Comment