Sorting Gridview Control ASP.NET


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



Copy this code and paste it in your HTML
  1. protected void grdVw_FileList_Sorting(object sender, GridViewSortEventArgs e)
  2. {
  3.  
  4. GridSorting(e.SortExpression, grdVw_FileList);
  5.  
  6. }
  7.  
  8. private void GridSorting(string column, GridView grid)
  9. {
  10. // Session["column"] = ""; clear this after ever search/load
  11. string column_ASC = column + " ASC";
  12. string column_DESC = column + " DESC";
  13.  
  14. DataTable dt = (DataTable)Session["fileList"];
  15.  
  16. if (Session["column"].ToString().ToUpper() == column_ASC.ToUpper())
  17. {
  18. DataView dv = new DataView(dt, "", column_DESC, DataViewRowState.CurrentRows);
  19. grid.DataSource = dv;
  20. grid.DataBind();
  21. Session["column"] = column_DESC;
  22. }
  23. else
  24. {
  25. DataView dv = new DataView(dt, "", column_ASC, DataViewRowState.CurrentRows);
  26. grid.DataSource = dv;
  27. grid.DataBind();
  28. Session["column"] = column_ASC;
  29. }
  30.  
  31. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.