Toggle Asc/Desc Sorting Gridview Columns


/ Published in: C#
Save to your folder(s)

When I first used this code, it was in the GridView_OnSorting event. This will sort the Sort Expression and Sort Direction in the ViewState. In the fill_grid() function I will pull these values from the ViewState to construct the sort.


Copy this code and paste it in your HTML
  1. SortDirection sortdir = SortDirection.Ascending;
  2.  
  3. if (ViewState["SortDirection"] != null)
  4. {
  5. if ((string)ViewState["sortexp"] == e.SortExpression)
  6. {
  7. sortdir = (SortDirection)ViewState["SortDirection"] == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending;
  8. }
  9. }
  10.  
  11. string sortexp = e.SortExpression;
  12. ViewState["sortexp"] = sortexp;
  13. ViewState["SortDirection"] = sortdir;
  14. fill_grid();

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.