title: how to do gridview sorting without data Source?
body: <p>i am trying to do sorting and paging for my gridview without datasource but when i click on the header of the gridview to sort it , nothing happen but when i click for paging is working fine and there is no error. how to do about it?</p>
  
  <p>this is my code: </p>
  
  <pre><code> protected void Page_Load(object sender, EventArgs e)
      {
          dbBind();
      }
  
      protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
      {
          DataTable dtSortTable = GridView1.DataSource as DataTable;
          if (dtSortTable != null)
          {
              DataView dvSortedView = new DataView(dtSortTable);
              dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString(e.SortDirection);
              GridView1.DataSource = dvSortedView;
              GridView1.DataBind();
          }
      }
  
      private string getSortDirectionString(SortDirection sortDirection)
      {
          string newSortDirection = String.Empty;
  
          if(sortDirection== SortDirection.Ascending)
          {
             newSortDirection = "ASC";
          }
          else
          {
             newSortDirection = "DESC";
          }
  
          return newSortDirection;
      }
  
      private void dbBind()
      {
  
          DataSet ds;
  
          string startdate = (string)(Session["startdate"]);
          string enddate = (string)(Session["enddate"]);
  
          var format = "dd/MM/yyyy";
          DateTime one = DateTime.ParseExact(startdate, format, CultureInfo.InvariantCulture);
          DateTime two = DateTime.ParseExact(enddate, format, CultureInfo.InvariantCulture);
  
  
          if (two &gt;= one)
          {
  
              SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=n;Integrated Security=True");
              //conn.Open();
              SqlCommand cmd = new SqlCommand("SELECT [AmountSpent], [TimeDate]=convert(nvarchar,timedate,103), [StallNo] FROM [StudentTransactions] WHERE TimeDate BETWEEN @one AND @two", conn);
              cmd.Parameters.AddWithValue("@one", one);
              cmd.Parameters.AddWithValue("@two", two);
  
              ds = new DataSet();
              using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
              {
                  sda.Fill(ds);
                  if (ds.Tables.Count &gt; 0)
                  {
                      DataView dv = ds.Tables[0].DefaultView;
                      if (ViewState["SortDirection"] != null)
                      {
                          sortDirection = ViewState["SortDirection"].ToString();
                      }
  
                      if (ViewState["SortExpression"] != null)
                      {
                          sortExpression = ViewState["SortExpression"].ToString();
                          dv.Sort = string.Concat(sortExpression, " ", sortDirection);
                      }
                      GridView1.DataSource = dv;
                      GridView1.DataBind();
                  }
              }  
              //GridView1.DataBind();
              //conn.Close();
          }
  
          else
          {
              GridView1.Visible = false;
              string strMsg = " Data not found for the choosen dates.";
              Response.Write("&lt;script&gt;alert('" + strMsg + "')&lt;/script&gt;");
          }
  
  
      }
  </code></pre>