Ganesh babu

Ganesh babu

  • NA
  • 40
  • 3k

Grid view sorting and paging after performing search

Apr 18 2019 4:26 AM
I am performing a search operation in GridView and getting results as expected, but after getting results, if I sort them or click on pagination, the page getting reloaded and sorting and pagination happening for the whole grid. I want sorting and paging should only work for search results which I have got.
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3. if (!IsPostBack)  
  4. {  
  5. bindgrid();  
  6. }  
  7. }  
  8.   
  9. protected void bindgrid()  
  10. {  
  11. ViewState["sortexp"] = "";  
  12. ViewState["orderby"] = "ASC";  
  13. Sort("");  
  14. int iTotalRecords = ((DataTable)(GridView1.DataSource)).Rows.Count;  
  15. lbldisplay.Text = "Total Products: " + iTotalRecords.ToString();  
  16. }  
  17.   
  18. protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)  
  19. {  
  20. GridView1.PageIndex = e.NewPageIndex;  
  21. GridView1.DataBind();  
  22. bindgrid();  
  23. }  
  24. public void Sort(string sortexp)  
  25. {  
  26. DataTable dt = (DataTable)bll.LoadProduct();  
  27. DataView DV = dt.DefaultView;if (sortexp != "")  
  28. {  
  29. DV.Sort = sortexp;  
  30. }  
  31. GridView1.DataSource = DV;  
  32. GridView1.DataBind();  
  33. }  
  34. protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)  
  35. {  
  36. if (ViewState["orderby"].ToString() == "ASC")  
  37. {  
  38. ViewState["orderby"] = "DESC";  
  39. ViewState["sortexp"] = e.SortExpression + " " + " DESC";  
  40. }else  
  41. {  
  42. ViewState["orderby"] = "ASC";  
  43. ViewState["sortexp"] = e.SortExpression + " " + " ASC";  
  44. }  
  45. Sort(ViewState["sortexp"].ToString());  
  46. }  

Answers (5)