title: sorting gridview in asp.net/c#
body: <p>I have a gridview which is defined as follows:</p>
  
  <pre><code>&lt;asp:GridView ID="ComponentGridView" runat="server" AutoGenerateColumns="true" OnPageIndexChanging="ComponentGridView_PageIndexChanging" OnSorting="ComponentGridView_Sorting"&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>and these are the methods that get invoked on ComponentGridView_Sorting</p>
  
  <pre><code>    protected void ComponentGridView_Sorting(object sender, GridViewSortEventArgs e)
      {
          DataTable dataTable = ComponentGridView.DataSource as DataTable;
  
          if (dataTable != null)
          {
              DataView dataView = new DataView(dataTable);
              dataView.Sort = e.SortExpression + " " + ConvertSortDirection(e.SortDirection);
  
              ComponentGridView.DataSource = dataView;
              ComponentGridView.DataBind();
          }
      }
  </code></pre>
  
  <p>and </p>
  
  <pre><code> private string ConvertSortDirection(SortDirection sortDirection)
      {
          string newSortDirection = String.Empty;
  
          switch (sortDirection)
          {
              case SortDirection.Ascending:
                  newSortDirection = "ASC";
                  break;
  
              case SortDirection.Descending:
                  newSortDirection = "DESC";
                  break;
          }
  
          return newSortDirection;
      }
  </code></pre>
  
  <p>But it seems like nothing is happening when i click on the column headers.</p>
  
  <p>What am i doing wrong ?</p>
  
  <p>Please help me</p>
  
  <p>Thanks in anticipation</p>
  
  <p>When i debugged the <strong>dataTable</strong> is null</p>
  
  <p>DataTable dataTable = ComponentGridView.DataSource as DataTable;</p>