title: Why does sorting does not work when I go to another paging?
body: <p>Still new and beginner at asp.net. I wonder why sorting does not work when i changed to another paging? I sorted in page 1, but when i go to page 2 the sort seems not working.</p>
  <p>What i want is when i click the header, it will sort all way to the last page and does not break when changing the paging.</p>
  <p>This code works, but when i changed the paging, the sort breaks or not working. I still see data that should be in page 1 not in page 2.</p>
  <p>tblaccount is a asp:gridview, Sorting and Paging is set to True.</p>
  <pre><code> protected void Page_Load(object sender, EventArgs e)
          {
              //populateCDA();
  
              if (!Page.IsPostBack)
              {
                  refreshdata();
              }
  
          }
  
          private void populateCDA()
          {
              DataTable table = new DataTable();
              // get the connection    
              using (SqlConnection conn = new SqlConnection(mc.defaultConnection))
              {
                  // write the sql statement to execute    
                  string sql = &quot;SELECT * FROM [dbo].[GETACCOUNTS]()&quot;;
                  // instantiate the command object to fire    
                  using (SqlCommand cmd = new SqlCommand(sql, conn))
                  {
                      // get the adapter object and attach the command object to it    
                      using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
                      {
                          // fire Fill method to fetch the data and fill into DataTable    
                          ad.Fill(table);
                      }
                  }
              }
              // specify the data source for the GridView    
              tblViewAccount.DataSource = table;
              // bind the data now    
              tblViewAccount.DataBind();
          }
  
          public void refreshdata()
          {
              
              SqlConnection con = new SqlConnection(mc.defaultConnection);
              SqlCommand cmd = new SqlCommand(&quot;SELECT * FROM [dbo].[GETACCOUNTS]()&quot;, con);
              SqlDataAdapter sda = new SqlDataAdapter(cmd);
              DataTable dt = new DataTable();
              sda.Fill(dt);
              tblViewAccount.DataSource = dt;
              tblViewAccount.DataBind();
              ViewState[&quot;dirState&quot;] = dt;
              ViewState[&quot;sortdr&quot;] = &quot;Asc&quot;;
  
  
          }
  
          protected void tblViewAccount_Sorting(object sender, GridViewSortEventArgs e)
          {
              DataTable dtrslt = (DataTable)ViewState[&quot;dirState&quot;];
              if (dtrslt.Rows.Count &gt; 0)
              {
                  if (Convert.ToString(ViewState[&quot;sortdr&quot;]) == &quot;Asc&quot;)
                  {
                      dtrslt.DefaultView.Sort = e.SortExpression + &quot; Desc&quot;;
                      ViewState[&quot;sortdr&quot;] = &quot;Desc&quot;;
                  }
                  else
                  {
                      dtrslt.DefaultView.Sort = e.SortExpression + &quot; Asc&quot;;
                      ViewState[&quot;sortdr&quot;] = &quot;Asc&quot;;
                  }
                  tblViewAccount.DataSource = dtrslt;
                  tblViewAccount.DataBind();
  
  
              }
          }
  
          protected void tblViewAccount_PageIndexChanging(object sender, GridViewPageEventArgs e)
          {
              tblViewAccount.PageIndex = e.NewPageIndex;
              tblViewAccount.DataBind();
          }
  </code></pre>