Gridsorting.aspx
<asp:GridView ID="grd" runat="server" AllowPaging="true" AllowSorting="true" PageSize="10"
AutoGenerateColumns="false" ShowFooter ="true"
DataKeyNames="id" OnSorting="grd_Sorting" OnRowEditing="grd_RowEditing"
onrowupdating="grd_RowUpdating" onrowcancelingedit="grd_RowCancelingEdit"
onpageindexchanging="grd_PageIndexChanging"
onrowdeleting="grd_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="Name" SortExpression="name" FooterStyle-Width="200px"
ItemStyle-Width="200px">
<ItemTemplate>
<asp:Label ID="lblname" Text='<%#Eval("name") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtname" Text='<%#Eval("name") %>' runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEditCame" ValidationGroup="Edit" runat="server"
SetFocusOnError="true" Display="Dynamic" ControlToValidate="txtname" ErrorMessage="Please , Enter Name."></asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtfname" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvFooteitynme" ValidationGroup="Footer" runat="server"
SetFocusOnError="true" Display="Dynamic" ControlToValidate="txtfname" ErrorMessage="Please , Enter City Name."></asp:RequiredFieldValidator>
</FooterTemplate>
<FooterStyle Width="200px"></FooterStyle>
<ItemStyle Width="200px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email" SortExpression="E_id" FooterStyle-Width="200px"
ItemStyle-Width="200px">
<ItemTemplate>
<asp:Label ID="lbleid" runat="server" Text='<%# Eval("E_id") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtemail" runat="server" Text='<%# Eval("E_id") %>' ></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEditCityname" ValidationGroup="Edit" runat="server"
SetFocusOnError="true" Display="Dynamic" ControlToValidate="txtemail" ErrorMessage="Please , Enter Email Id"></asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtfemail" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="vFooterCityname" ValidationGroup="Footer" runat="server"
SetFocusOnError="true" Display="Dynamic" ControlToValidate="txtfemail" ErrorMessage="Please , Enter Email Id"></asp:RequiredFieldValidator>
</FooterTemplate>
<FooterStyle Width="200px"></FooterStyle>
<ItemStyle Width="200px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile" SortExpression="Mobile" FooterStyle-Width="200px"
ItemStyle-Width="200px">
<ItemTemplate>
<asp:Label ID="lblMObile" Text='<%#Bind("mobile")%>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditMObile" Text='<%#Bind("Mobile")%>' runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="mobilename" ValidationGroup="Edit" runat="server"
SetFocusOnError="true" Display="Dynamic" ControlToValidate="txtEditMObile"
ErrorMessage="Please , Enter Mobile No."></asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtmobile" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="mobile" ValidationGroup="Footer" runat="server"
SetFocusOnError="true" Display="Dynamic" ControlToValidate="txtmobile" ErrorMessage="Please , Enter Mobile NO."></asp:RequiredFieldValidator>
</FooterTemplate>
<FooterStyle Width="200px"></FooterStyle>
<ItemStyle Width="200px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="City Name" SortExpression="City" FooterStyle-Width="200px"
ItemStyle-Width="200px">
<ItemTemplate>
<asp:Label ID="lblCityName" Text='<%#Bind("City")%>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditCityName" Text='<%#Bind("City")%>' runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvityname" ValidationGroup="Edit" runat="server"
SetFocusOnError="true" Display="Dynamic" ControlToValidate="txtEditCityName"
ErrorMessage="Please , Enter City Name."></asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtCityName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvFooterCitame" ValidationGroup="Footer" runat="server"
SetFocusOnError="true" Display="Dynamic" ControlToValidate="txtCityName" ErrorMessage="Please , Enter City Name."></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>
AutoGenerateColumns="false" ShowFooter ="true"
DataKeyNames="id" OnSorting="grd_Sorting" OnRowEditing="grd_RowEditing"
onrowupdating="grd_RowUpdating" onrowcancelingedit="grd_RowCancelingEdit"
onpageindexchanging="grd_PageIndexChanging"
onrowdeleting="grd_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="Name" SortExpression="name" FooterStyle-Width="200px"
ItemStyle-Width="200px">
<ItemTemplate>
<asp:Label ID="lblname" Text='<%#Eval("name") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtname" Text='<%#Eval("name") %>' runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEditCame" ValidationGroup="Edit" runat="server"
SetFocusOnError="true" Display="Dynamic" ControlToValidate="txtname" ErrorMessage="Please , Enter Name."></asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtfname" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvFooteitynme" ValidationGroup="Footer" runat="server"
SetFocusOnError="true" Display="Dynamic" ControlToValidate="txtfname" ErrorMessage="Please , Enter City Name."></asp:RequiredFieldValidator>
</FooterTemplate>
<FooterStyle Width="200px"></FooterStyle>
<ItemStyle Width="200px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email" SortExpression="E_id" FooterStyle-Width="200px"
ItemStyle-Width="200px">
<ItemTemplate>
<asp:Label ID="lbleid" runat="server" Text='<%# Eval("E_id") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtemail" runat="server" Text='<%# Eval("E_id") %>' ></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEditCityname" ValidationGroup="Edit" runat="server"
SetFocusOnError="true" Display="Dynamic" ControlToValidate="txtemail" ErrorMessage="Please , Enter Email Id"></asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtfemail" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="vFooterCityname" ValidationGroup="Footer" runat="server"
SetFocusOnError="true" Display="Dynamic" ControlToValidate="txtfemail" ErrorMessage="Please , Enter Email Id"></asp:RequiredFieldValidator>
</FooterTemplate>
<FooterStyle Width="200px"></FooterStyle>
<ItemStyle Width="200px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile" SortExpression="Mobile" FooterStyle-Width="200px"
ItemStyle-Width="200px">
<ItemTemplate>
<asp:Label ID="lblMObile" Text='<%#Bind("mobile")%>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditMObile" Text='<%#Bind("Mobile")%>' runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="mobilename" ValidationGroup="Edit" runat="server"
SetFocusOnError="true" Display="Dynamic" ControlToValidate="txtEditMObile"
ErrorMessage="Please , Enter Mobile No."></asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtmobile" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="mobile" ValidationGroup="Footer" runat="server"
SetFocusOnError="true" Display="Dynamic" ControlToValidate="txtmobile" ErrorMessage="Please , Enter Mobile NO."></asp:RequiredFieldValidator>
</FooterTemplate>
<FooterStyle Width="200px"></FooterStyle>
<ItemStyle Width="200px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="City Name" SortExpression="City" FooterStyle-Width="200px"
ItemStyle-Width="200px">
<ItemTemplate>
<asp:Label ID="lblCityName" Text='<%#Bind("City")%>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditCityName" Text='<%#Bind("City")%>' runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvityname" ValidationGroup="Edit" runat="server"
SetFocusOnError="true" Display="Dynamic" ControlToValidate="txtEditCityName"
ErrorMessage="Please , Enter City Name."></asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtCityName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvFooterCitame" ValidationGroup="Footer" runat="server"
SetFocusOnError="true" Display="Dynamic" ControlToValidate="txtCityName" ErrorMessage="Please , Enter City Name."></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>
gridsorting.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DAL;
public partial class gridsorting : System.Web.UI.Page
{
DataTable dt = new DataTable();
Dal odal = new Dal();
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fill();
}
}
public void fill()
{
dt = odal.selectbyquerydt("select * from registeruser");
grd.DataSource = dt;
grd.DataBind();
}
public SortDirection dir
{
get
{
if (ViewState["dirState"] == null)
{
ViewState["dirState"] = SortDirection.Ascending;
}
return (SortDirection)ViewState["dirState"];
}
set
{
ViewState["dirState"] = value;
}
}
protected void grd_Sorting(object sender, GridViewSortEventArgs e)
{
fill();
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();
}
}
protected void grd_RowEditing(object sender, GridViewEditEventArgs e)
{
grd.EditIndex = 0;
}
protected void btninsert_Click(object sender, EventArgs e)
{
TextBox Name = (TextBox)grd.FooterRow.FindControl("txtfName") as TextBox;
TextBox Email = (TextBox)grd.FooterRow.FindControl("txtfemail") as TextBox;
TextBox city = (TextBox)grd.FooterRow.FindControl("txtCityName") as TextBox;
TextBox mobile = (TextBox)grd.FooterRow.FindControl("txtmobile") as TextBox;
odal.updatedata("insert into registeruser (name,e_id,Mobile,city) values('" + Name.Text + "','" + Email.Text + "','" + mobile.Text + "','" + city.Text + "')");
fill();
}
protected void grd_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
grd.EditIndex = e.RowIndex;
int id = int.Parse(grd.DataKeys[grd.EditIndex].Value.ToString());
TextBox Name = (TextBox)grd.Rows[e.RowIndex].FindControl("txtName") as TextBox;
TextBox Email = (TextBox)grd.Rows[e.RowIndex].FindControl("txtemail") as TextBox;
TextBox city = (TextBox)grd.Rows[e.RowIndex].FindControl("txtEditCityName") as TextBox;
TextBox mobile = (TextBox)grd.Rows[e.RowIndex].FindControl("txtEditMObile") as TextBox;
string str = "update registeruser set name='" + Name.Text + "',e_id='" + Email.Text + "',city='" + city.Text + "',mobile='" + mobile.Text + "' where id =" + id;
odal.updatedata(str);
grd.EditIndex = -1;
fill();
}
protected void grd_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grd.EditIndex = -1;
fill();
}
protected void grd_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grd.PageIndex = e.NewPageIndex;
fill();
}
protected void grd_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
grd.EditIndex = e.RowIndex;
int id = int.Parse(grd.DataKeys[grd.EditIndex].Value.ToString ());
odal.updatedata("delete from registeruser where id="+id);
fill();
}
protected void grd_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
grd.EditIndex = e.RowIndex;
int id = int.Parse(grd.DataKeys[grd.EditIndex].Value.ToString ());
odal.updatedata("delete from registeruser where id="+id);
fill();
}
Very good Article
ReplyDelete