Insert Update Delete and Sorting In Grid view Control Without Db
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
table
{
border: 1px solid #ccc;
}
table th
{
background-color: #F7F7F7;
color: #333;
font-weight: bold;
}
table th, table td
{
padding: 5px;
border-color: #ccc;
}
</style>
<asp:GridView ID="grd" runat="server" AllowPaging="true" AllowSorting="true" PageSize="10"
AutoGenerateColumns="false"
ShowFooter="true"
DataKeyNames="id"
OnRowCancelingEdit="grd_RowCancelingEdit"
OnRowDeleting="grd_RowDeleting"
OnRowEditing="grd_RowEditing"
OnRowUpdating="grd_RowUpdating"
OnSorting="grd_Sorting">
<Columns>
<asp:TemplateField HeaderText="product"
SortExpression="product"
FooterStyle-Width="200px"
ItemStyle-Width="200px">
<ItemTemplate>
<asp:Label ID="lblproduct" Text='<%#Eval("product") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txteproduct" Text='<%#Eval("product") %>' runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEditCame" ValidationGroup="Edit" runat="server"
SetFocusOnError="true"
Display="Dynamic"
ControlToValidate="txteproduct"
ErrorMessage="Please
, Enter product Name."></asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtproduct" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvFooteitynme" ValidationGroup="Footer" runat="server"
SetFocusOnError="true"
Display="Dynamic"
ControlToValidate="txtproduct"
ErrorMessage="Please
, Enter product Name."></asp:RequiredFieldValidator>
</FooterTemplate>
<FooterStyle
Width="200px"></FooterStyle>
<ItemStyle
Width="200px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="QTY"
SortExpression="qty"
FooterStyle-Width="200px"
ItemStyle-Width="200px">
<ItemTemplate>
<asp:Label ID="lblqty" runat="server" Text='<%# Eval("qty") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txteqty" runat="server" Text='<%# Eval("qty") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEditCityname" ValidationGroup="Edit" runat="server"
SetFocusOnError="true"
Display="Dynamic"
ControlToValidate="txteqty"
ErrorMessage="Please
, Enter qty"></asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtqty" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="vFooterCityname" ValidationGroup="Footer" runat="server"
SetFocusOnError="true"
Display="Dynamic"
ControlToValidate="txtqty"
ErrorMessage="Please
, Enter qty"></asp:RequiredFieldValidator>
</FooterTemplate>
<FooterStyle
Width="200px"></FooterStyle>
<ItemStyle
Width="200px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Rate"
SortExpression="Rate"
FooterStyle-Width="200px"
ItemStyle-Width="200px">
<ItemTemplate>
<asp:Label ID="lblRate" Text='<%# Eval("Rate")%>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditRate" Text='<%#Eval("Rate")%>' runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="Ratename" ValidationGroup="Edit" runat="server" SetFocusOnError="true"
Display="Dynamic"
ControlToValidate="txtEditRate"
ErrorMessage="Please
, Enter Rate."></asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtRate" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="Rate" ValidationGroup="Footer" runat="server" SetFocusOnError="true"
Display="Dynamic"
ControlToValidate="txtRate"
ErrorMessage="Please
, Enter Rate."></asp:RequiredFieldValidator>
</FooterTemplate>
<FooterStyle
Width="200px"></FooterStyle>
<ItemStyle
Width="200px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount"
SortExpression="Amount"
FooterStyle-Width="200px"
ItemStyle-Width="200px">
<ItemTemplate>
<asp:Label ID="lblAmount" Text='<%#Bind("Amount")%>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEdittxtAmount" Text='<%#Bind("Amount")%>' runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvityname" ValidationGroup="Edit" runat="server"
SetFocusOnError="true"
Display="Dynamic"
ControlToValidate="txtEdittxtAmount"
ErrorMessage="Please ,
Enter Amount."></asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAmount" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvFooterCitame" ValidationGroup="Footer" runat="server"
SetFocusOnError="true"
Display="Dynamic"
ControlToValidate="txtAmount"
ErrorMessage="Please
, Enter Amount."></asp:RequiredFieldValidator>
</FooterTemplate>
<FooterStyle
Width="200px"></FooterStyle>
<ItemStyle
Width="200px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:Button ID="btnedit" runat="server" Text="Edit" CommandName="Edit" />
<asp:Button ID="btndelete" runat="server" Text="Delete" CommandName="Delete" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btnupdate" runat="server" Text="Update" ValidationGroup="Edit" CommandName="update" />
<asp:Button ID="btncancel" runat="server" Text="Cancel" CommandName="Cancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="btninsert" runat="server" Text="Insert" ValidationGroup="Footer"
OnClick="btninsert_Click"
/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class test :
System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new
DataTable();
dt.Columns.AddRange(new DataColumn[5] { new
DataColumn("Id"),
new DataColumn("product"), new
DataColumn("qty"),
new DataColumn("Rate"), new
DataColumn("Amount")
});
dt.Rows.Add(1, "Jewelry", "2", "2000",
"4000");
dt.Rows.Add(2, "Dimond", "3", "300",
"900");
dt.Rows.Add(3, "Gold", "1", "20000",
"20000");
dt.Rows.Add(4, "Silver", "5", "4500",
"22500");
ViewState["dt"] = dt;
this.BindGrid();
}
}
protected void
BindGrid()
{
grd.DataSource = ViewState["dt"]
as DataTable;
grd.DataBind();
}
protected void
grd_RowEditing(object sender, GridViewEditEventArgs e)
{
grd.EditIndex = e.NewEditIndex;
this.BindGrid();
}
protected void
grd_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox pname = (TextBox)grd.Rows[e.RowIndex].FindControl("txteproduct") as
TextBox;
TextBox qty = (TextBox)grd.Rows[e.RowIndex].FindControl("txteqty") as
TextBox;
TextBox rate = (TextBox)grd.Rows[e.RowIndex].FindControl("txtEditRate") as
TextBox;
TextBox Amount = (TextBox)grd.Rows[e.RowIndex].FindControl("txtEdittxtAmount") as TextBox;
DataTable dt = ViewState["dt"]
as DataTable;
dt.Rows[e.RowIndex]["product"]
= pname.Text;
dt.Rows[e.RowIndex]["qty"]
= qty.Text;
dt.Rows[e.RowIndex]["Rate"] = rate.Text;
dt.Rows[e.RowIndex]["Amount"]
= Amount.Text;
ViewState["dt"] = dt;
grd.EditIndex = -1;
this.BindGrid();
}
protected void
grd_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grd.EditIndex = -1;
//this.BindGrid();
}
protected void
grd_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
grd.EditIndex = e.RowIndex;
DataTable dt = new
DataTable();
dt =
(DataTable)ViewState["dt"];
int id = int.Parse(grd.DataKeys[grd.EditIndex].Value.ToString());
for (int i = 0; i
< dt.Rows.Count; i++)
{
if (int.Parse(dt.Rows[i]["id"].ToString()) == id)
{
dt.Rows[i].Delete();
break;
}
}
grd.EditIndex = -1;
grd.DataSource = dt;
grd.DataBind();
ViewState["dt"] = dt;
}
protected void
btninsert_Click(object sender, EventArgs e)
{
TextBox pname = (TextBox)grd.FooterRow.FindControl("txtproduct") as
TextBox;
TextBox qty = (TextBox)grd.FooterRow.FindControl("txtqty") as
TextBox;
TextBox rate = (TextBox)grd.FooterRow.FindControl("txtRate") as
TextBox;
TextBox Amount = (TextBox)grd.FooterRow.FindControl("txtAmount") as
TextBox;
DataTable dt = new
DataTable();
dt =
(DataTable)ViewState["dt"];
dt.Rows.Add(dt.Rows.Count + 1, pname.Text, qty.Text, rate.Text,
Amount.Text);
grd.DataSource = dt;
grd.DataBind();
ViewState["dt"] = dt;
}
protected void
grd_Sorting(object sender, GridViewSortEventArgs e)
{
BindGrid();
DataTable dt = new
DataTable();
dt =
grd.DataSource as DataTable;
{
string SortDir = string.Empty;
if (dir == SortDirection.Ascending)
{
dir = SortDirection.Descending;
SortDir = "Desc";
}
else
{
dir = SortDirection.Ascending;
SortDir = "Asc";
}
DataView sortedView = new
DataView(dt);
sortedView.Sort = e.SortExpression + "
" + SortDir;
grd.DataSource = sortedView;
grd.DataBind();
}
}
public SortDirection
dir
{
get
{
if (ViewState["dirState"]
== null)
{
ViewState["dirState"] = SortDirection.Ascending;
}
return (SortDirection)ViewState["dirState"];
}
set
{
ViewState["dirState"] = value;
}
}
}
Comments
Post a Comment