Sorting Gridview control on header click with pagination Asp.net C#

2
Our Reader Score
[Total: 0 Average: 0]
 Overview: Asp.net sorting Gridview control  on column header click in Ascending or Descending order,  binding with DataTable Asp.net c# with pagination. In this article, I am going to give some overview on how to  SORT the data when user click on Gridview Header cell  (i.e) Sorting Gridview control in Asp.net.

Also, I recommend you have a look at my Previous Article about Gridview Add/Update/Delete functionality ( CRUD ) or Gridview row color change based on data.

Though there are a lot of articles on web which explains paging and sorting, there’s some reasons which differ this article from other articles. ie Here Gridview control is bind with Datatable as in another article you found using ObjectDataSource also Paging not supported.

Recently I was working with GridView control and I was having manipulated Datatable as a DataSource to it. While solving this, I developed a good piece of code which I do feel will help most of you in your development activity.


HTML Markup :

  1. Set AllowPaging="True" and AllowSorting="True" Properties of GridView to enable paging and sorting respectively.
  2. Set PageSize property to mention how many records will be displayed on each page.
  3. Set SortExpression property of each column.

 


Code Behind: step by step for sorting Girdview


 

Now here we store the direction in ViewState i.e. ( Ascending or Descending for sorting the gridview).

 

You must check these articles:

Hope you enjoyed this tutorial. If you have any recommendations, please let us know what you think in the comment section below! See you again next time!


Get your Printable Copy

2 COMMENTS

  1. Hi,
    I’ve just had to make a small alteration to get it to work with with me;
    On the gvSortingPaging_PageIndexChanging method I’ve had to change the line
    gvSortingPaging.DataSource = Session[“objects”];
    to
    gvSortingPaging.DataSource = Session[“objects”] ?? PopulateGridView();
    As it may not of been sorted it yet, so the data wouldn’t of been in the session.

    Thanks
    Mark.

LEAVE A REPLY