title: Manually Sorting Data in Gridview in asp.Net Webforms
body: <p>I am having some difficult manually sorting data on a gridview. I used a dataset and when set the AllowSort to true and also wrote the code to handle the sort based on the guide given on <a href="https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sorting.aspx" rel="nofollow">https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sorting.aspx</a>  . However, when I run my code, the data displays but when I click the header of each column, nothing happens.
  here is my code</p>
  
  <pre><code> protected void Page_Load(object sender, EventArgs e)
      {
  
  
         string connstring = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString;
          SqlConnection conn = new SqlConnection(connstring);
          conn.Open();
          SqlCommand comm = conn.CreateCommand();
  
  
      comm.CommandText = "SELECT Count(Student.StudentID) AS StdCount, Schools.Name, Schools.StartDate, School.SchoolFees FROM Schools INNER JOIN Students ON Schools.SchoolID = Student.SchoolID WHERE School.Active = 1 GROUP BY Schools.Name, Schools.StartDate, Schools.SchoolFess ORDER BY Schools.Name ASC";
          SqlDataAdapter da = new SqlDataAdapter(comm);
  
          DataSet ds = new DataSet();
  
           da.Fill(ds);
  
          if (ds.Tables.Count &gt; 0)
          {
              DataTable dt = ds.Tables[0];
  
              ViewState["datable"] = dt;
          }
  
  
  
          GridView1.DataSource = ds;
          GridView1.DataBind(); 
  
      }
  protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
      {
  
          //Retrieve the table from the session object.
          DataTable dt = (DataTable)ViewState["datable"];
  
          if (dt != null)
          {
  
              //Sort the data.
              dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
              GridView1.DataSource = ViewState["datable"];
              GridView1.DataBind();
          }
  
      }
  
      private string GetSortDirection(string column)
      {
  
          // By default, set the sort direction to ascending.
          string sortDirection = "ASC";
  
          // Retrieve the last column that was sorted.
          string sortExpression = ViewState["SortExpression"] as string;
  
          if (sortExpression != null)
          {
              // Check if the same column is being sorted.
              // Otherwise, the default value can be returned.
              if (sortExpression == column)
              {
                  string lastDirection = ViewState["SortDirection"] as string;
                  if ((lastDirection != null) &amp;&amp; (lastDirection == "ASC"))
                  {
                      sortDirection = "DESC";
                  }
              }
          }
  
          // Save new values in ViewState.
          ViewState["SortDirection"] = sortDirection;
          ViewState["SortExpression"] = column;
  
          return sortDirection;
      }
  </code></pre>
  
  <p>Any help will be appreciated. Thanks</p>