Source:
<asp:GridView ID="gridUserMaster" runat="server" AllowSorting="True" OnSorting="CustomersGridView_Sorting"
CS:
protected void CustomersGridView_Sorting(Object sender, GridViewSortEventArgs e)
{
try
{
DataTable datatable = (DataTable)ViewState["gridview_datasource"];
if (datatable != null)
{
DataView dataview = new DataView(datatable);
if ((ViewState["sortdirection"] == null || (SortDirection)ViewState["sortdirection"] == SortDirection.Descending))
ViewState["sortdirection"] = SortDirection.Ascending;
else
ViewState["sortdirection"] = SortDirection.Descending;
dataview.Sort = e.SortExpression + " " + convertsortdirection((SortDirection)ViewState["sortdirection"]);
gridUserMaster.DataSource = dataview;
gridUserMaster.DataBind();
}
}
catch (Exception ex)
{
Err.ErrorLog(Server.MapPath("Logs/ErrorLog"), ex.Message);
ClientScript.RegisterStartupScript(this.GetType(), "msg", "alert('" + ex.Message.ToString() + "');", true);
}
}
Get more detailed description on MSDN:
GridView Examples for ASP.NET 2.0: Paging and Sorting the GridView's Data[
^]
Sorting Data in a GridView Web Server Control[
^]
Similar thread:
How to sort data in GridView in asp.net[
^]
How to sort data into GridView[
^]