title: GridView sorting & paging
body: <p>I would like to display simple gridview on my page and provide sorting and paging functionality to it. Sorting and paging individually is working ok, but the combination of both does not. For example if I sort the first column descending and then go to page number two, then I see the second page of data with default sorting (ascending).</p>
  
  <p>I relied heavily on the code from this question: <a href="https://stackoverflow.com/questions/250037/gridview-sorting-sortdirection-always-ascending">GridView sorting: SortDirection always Ascending</a>, but the problem remains. Also - as it seems I have to change my code so that instead of Session object ViewState is used, have to figure this out also...</p>
  
  <p>My code, simplified, with only two columns:</p>
  
  <p>aspx:</p>
  
  <pre><code>&lt;asp:GridView ID="dgvView" runat="server" AutoGenerateColumns="false" AllowPaging="true"
      PageSize="10" AllowSorting="True" OnPageIndexChanging="DgvViewPageIndexChanging" OnSorting="OnSort"&gt;
      &lt;Columns&gt;
          &lt;asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"&gt;
              &lt;ItemStyle /&gt;
          &lt;/asp:BoundField&gt;
          &lt;asp:BoundField DataField="BirthDate" HeaderText="Birth date" DataFormatString="{0:dd.MM.yyyy}"
              SortExpression="BirthDate"&gt;
              &lt;ItemStyle /&gt;
          &lt;/asp:BoundField&gt;
      &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>And codebehind:</p>
  
  <pre><code>public partial class TestPage :Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {
              DisplayData();
          }
      }
  
      private void DisplayData()
      {
          Session["TableView"] = GetUsers();
          dgvView.DataSource = Session["TableView"];
          dgvView.DataBind();
      }
  
      protected void DgvViewPageIndexChanging(object sender, GridViewPageEventArgs e)
      {
          dgvView.PageIndex = e.NewPageIndex;
          DisplayData();
      }
  
      private List&lt;MyUser&gt; GetUsers()
      {
          var users = new List&lt;MyUser&gt;();
          for (int i = 0; i &lt; 100; i++)
          {
              users.Add(new MyUser("Name" + i.ToString().PadLeft(2, '0'), new DateTime(2000, 1, 1).AddDays(i)));
          }
          return users;
      }
  
      private class MyUser
      {
          public string Name { get; private set; }
          public DateTime BirthDate { get; private set; }
  
          public MyUser(string name, DateTime birthDate)
          {
              Name = name;
              BirthDate = birthDate;
          }
      }
  
      protected void OnSort(object sender, GridViewSortEventArgs e)
      {
          Func&lt;MyUser, object&gt; f;
          if (e.SortExpression == "Name") f = u =&gt; u.Name;
          else f = u =&gt; u.BirthDate;
  
          dgvView.DataSource = Sort((List&lt;MyUser&gt;)Session["TableView"], f, GetSortDirection(e.SortExpression));
          dgvView.DataBind();
      }
  
      private List&lt;MyUser&gt; Sort&lt;T&gt;(IEnumerable&lt;MyUser&gt; user, Func&lt;MyUser, T&gt; f, SortDirection sortDirection)
      {
          if (sortDirection == SortDirection.Ascending) return user.OrderBy(f).ToList();
          return user.OrderByDescending(f).ToList();
      }
  
      private SortDirection GetSortDirection(string column)
      {
          string sessionVariable = "TableSort" + column;
          SortDirection sortDirection;
  
          if (Session[sessionVariable] == null)
          {
              sortDirection = SortDirection.Ascending;
          }
          else if ((SortDirection)Session[sessionVariable] == SortDirection.Ascending)
          {
              sortDirection = SortDirection.Descending;
          }
          else
          {
              sortDirection = SortDirection.Ascending;
          }
  
          Session[sessionVariable] = sortDirection;
          return sortDirection;
      }
  }
  </code></pre>