Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sorting and paging with gridview asp.net

I'm trying to get a gridview to sort and page manually with no success.

The problem is that when a user clicks the column they want to sort, it sorts that page, but doesn't sort the datasource (dataview) behind the gridview. So when they progress to a different page, their sort is lost. Pretty much I'm looking for a sort that will actually sort the datasource behind the gridview. Here is what I have so far:

protected void GridView_OnSort(object sender, GridViewSortEventArgs e)     {         String sortExpression = e.SortExpression;          if (GridViewSortDirection == SortDirection.Ascending)         {             DataView myDataView = new DataView(mybll.GetItemsOrdered());             myDataView.Sort = sortExpression + " DESC";             GridView.DataSource = myDataView;             GridView.DataBind();         }         else         {             DataView myDataView = new DataView(mybll.GetItemsOrdered());             myDataView.Sort = sortExpression + " ASC";             GridView.DataSource = myDataView;             GridView.DataBind();         }     } 

Any help would be appreciated. Thanks.

like image 233
dangerisgo Avatar asked Mar 31 '09 19:03

dangerisgo


People also ask

What is paging in GridView in ASP NET?

GridView is one of the most common tools for displaying data in a grid format in ASP.NET. When the data becomes large, paging helps the users to view chunks of data and also increases page load time.

What is sort expression in GridView?

By default, the GridView control sorts a single column at a time. The sort expression simply contains the name of the field to sort. You can also sort multiple columns at a time by programmatically setting this property to a comma-separated list of field names.


1 Answers

Save your sorting order in a ViewState.

private const string ASCENDING = " ASC"; private const string DESCENDING = " DESC";  public SortDirection GridViewSortDirection {     get     {         if (ViewState["sortDirection"] == null)             ViewState["sortDirection"] = SortDirection.Ascending;          return (SortDirection) ViewState["sortDirection"];                     }     set { ViewState["sortDirection"] = value; }  }  protected void GridView_Sorting(object sender, GridViewSortEventArgs e) {     string sortExpression = e.SortExpression;      if (GridViewSortDirection == SortDirection.Ascending)     {         GridViewSortDirection = SortDirection.Descending;         SortGridView(sortExpression, DESCENDING);     }     else     {         GridViewSortDirection = SortDirection.Ascending;         SortGridView(sortExpression, ASCENDING);      }     }  private void SortGridView(string sortExpression,string direction) {     //  You can cache the DataTable for improving performance     DataTable dt = GetData().Tables[0];       DataView dv = new DataView(dt);      dv.Sort = sortExpression + direction;               GridView1.DataSource = dv;     GridView1.DataBind();          } 

Why you don't want to use existing sorting functionality? You can always customize it.

Sorting Data in a GridView Web Server Control at MSDN

Here is an example with customization:

http://www.netomatix.com/development/GridViewSorting.aspx

like image 185
Konstantin Tarkus Avatar answered Sep 18 '22 23:09

Konstantin Tarkus