.aspx code:
<div>
<asp:GridView ID="gvLocation" runat="server" AutoGenerateColumns="false"
AllowPaging="true" AllowSorting="true" onsorting="gvLocation_Sorting"
onpageindexchanging="gvLocation_PageIndexChanging" >
<Columns>
<asp:BoundField HeaderText="Region Name" DataField="RegionName" SortExpression="RegionName" />
<asp:BoundField HeaderText="Area Name" DataField="AreaName" SortExpression="AreaName" />
</Columns>
</asp:GridView>
</div>
Here is .aspx.cs code
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;
public partial class AutoComplete : System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
BindGridView();
}
}
protected void BindGridView()
{
DataTable
dtLocation = null;
if
(Session["vwLocation"] == null)
{
clsDAL
objDal = new clsDAL();
string
query = "SELECT RegionName, AreaName FROM TableA A INNER JOIN
TableB R ON A.RegionID = R.RegionID";
dtLocation =
objDal.GetDataTable(query);
if
(dtLocation.Rows.Count > 0)
{
Session["vwLocation"] = dtLocation.DefaultView;
gvLocation.DataSource =
dtLocation;
gvLocation.DataBind();
}
}
else
{
DataView
dvLocation = (DataView)Session["vwLocation"];
gvLocation.DataSource = dvLocation;
gvLocation.DataBind();
}
}
protected void gvLocation_Sorting(object
sender, GridViewSortEventArgs e)
{
string
sortExpression = string.Empty;
if
(ViewState["sortExpression"] == null)
{
ViewState["sortExpression"]
= "ASC";
sortExpression = e.SortExpression+" ASC";
}
else if (ViewState["sortExpression"].Equals("ASC"))
{
ViewState["sortExpression"]
= "DESC";
sortExpression = e.SortExpression +
" DESC";
}
else
{
ViewState["sortExpression"]
= "ASC";
sortExpression = e.SortExpression +
" ASC";
}
DataView
dvLocation = (DataView)Session["vwLocation"];
dvLocation.Sort = sortExpression;
BindGridView();
}
protected void gvLocation_PageIndexChanging(object sender, GridViewPageEventArgs
e)
{
gvLocation.PageIndex = e.NewPageIndex;
BindGridView();
}
Comments