- Step 1: Populate the ViewState with the datasource of the
GridView
in the !Page.IsPostback
event. - Step 2: In the
GridView
, set AllowSorting="true"
. - Step 3: Add
sortexpression="ColumnName"
. - Step 4: Handle the
OnSorting
event of the grid on the server side as follows:
protected void grdRoomTypeMaster_OnSorting(object sender, GridViewSortEventArgs e)
{
DataTable dtGridData = ViewState["grdDataSource"] as DataTable;
DataView dvGridDataView = dtGridData.DefaultView;
string strSortOrder = "";
if (ViewState["SortOrder"]==null)
{
ViewState["SortOrder"] = "asc";
}
if (ViewState["SortOrder"].ToString() == "asc")
{
ViewState["SortOrder"] = "desc";
strSortOrder = "desc";
}
else if (ViewState["SortOrder"].ToString() == "desc")
{
ViewState["SortOrder"] = "asc";
strSortOrder = "asc";
}
dvGridDataView.Sort = e.SortExpression + " " + strSortOrder;
dtGridData = dvGridDataView.ToTable();
grdRoomTypeMaster.DataSource = dtGridData;
grdRoomTypeMaster.DataBind();
}
Software developer by profession, working for a service and product based organisation in India.
Career graph:
Software Programmer since 2002.
Web Developer in ASP.NET since 2004.
Interests:
I love reading the blogs and articles of technology experts. I love codeproject and stackoverflow .
I love to share knowledge and help the programmers. I appreciate if some body corrects my code or my concepts which helps me learn.