title: Sorting in gridview is not working
body: <p>I am trying Sorting functionality in Grid view but its not working.Can some body help?</p>
  
  <p>Code:</p>
  
  <pre><code>  private string ConvertSortDirectionToSql(SortDirection sortDirection)
  {
      string newSortDirection = String.Empty;
  
      switch (sortDirection)
      {
          case SortDirection.Ascending:
              newSortDirection = "ASC";
              break;
  
          case SortDirection.Descending:
              newSortDirection = "DESC";
              break;
      }
  
      return newSortDirection;
  }
  protected DataSet FillDataSet()
  {
      string source = "Database=GridTest;Server=Localhost;Trusted_Connection=yes";
      con = new SqlConnection(source);
      cmd = new SqlCommand("proc_mygrid", con);
      ds = new DataSet();
      da = new SqlDataAdapter(cmd);
      da.Fill(ds);
      GridView1.DataSource = ds;
      GridView1.DataBind();
  
      return ds;
  
  
  }
   protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
  {
      DataTable dt = GridView1.DataSource as DataTable;
      if (dt != null)
      {
          DataView dv = new DataView(dt);
          dv.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
          GridView1.DataSource = dv;
          GridView1.DataBind();
     }
  </code></pre>
  
  <p>Here dt is coming null.why? pls help thanks.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <pre><code>enter code here  &lt;asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
      GridLines="None" AllowPaging="true" AllowSorting="true" PageSize="12" 
      onpageindexchanging="GridView1_PageIndexChanging" 
      onsorting="GridView1_Sorting"&gt;
  </code></pre>
  
  <p><strong>EDIT(Total code)</strong></p>
  
  <pre><code>public partial class _Default : System.Web.UI.Page 
  {
      SqlConnection con;
      SqlCommand cmd;
      DataSet ds;
      SqlDataAdapter da;
      protected void Page_Load(object sender, EventArgs e)
      {
  
      }
  
      private string ConvertSortDirectionToSql(SortDirection sortDirection)
      {
          string newSortDirection = String.Empty;
  
          switch (sortDirection)
          {
              case SortDirection.Ascending:
                  newSortDirection = "ASC";
                  break;
  
              case SortDirection.Descending:
                  newSortDirection = "DESC";
                  break;
          }
  
          return newSortDirection;
      }
      protected DataSet FillDataSet()
      {
          string source = "Database=GridTest;Server=Localhost;Trusted_Connection=yes";
          con = new SqlConnection(source);
          cmd = new SqlCommand("proc_mygrid", con);
          ds = new DataSet();
          da = new SqlDataAdapter(cmd);
          da.Fill(ds);
          GridView1.DataSource = ds;
          GridView1.DataBind();
  
          return ds;
  
  
      }
      protected void GetValues(object sender, EventArgs e)
      {
          FillDataSet();
      }
  
      protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
      {
         int newPagenumber = e.NewPageIndex;
         GridView1.PageIndex = newPagenumber;
         GridView1.DataSource = FillDataSet();
         GridView1.DataBind();
  
      }
  
  
      protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
      {
  
          DataSet ds = FillDataSet();
          DataTable dt = ds.Tables[0];
          if (dt != null)
          {
              dt.DefaultView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
              GridView1.DataSource = dt;
              GridView1.DataBind();
          }
  </code></pre>