ASP.NET Gridview Sorting not sorting
<p>I am having issues with the sorting on my Gridview. It always seems to be in one Sort Order i.e. Ascending? Never switches to Desc?</p>

<p>I have ran through the debug process and cannot figure out where am going wrong?</p>

<p>Here is my code, also am using pagination so I am not sure if it is preventing it from working properly?</p>

<pre><code>protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {        
    PopulateProductClass();
    PopulateProduct();
  }
  PopulateOrderList();
}

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 void gvOrderItems_Sorting(object sender, GridViewSortEventArgs e)
{   

  DataTable dt = new DataTable();
  dt.Columns.Add("OrderDate", typeof(string));
  dt.Columns.Add("OrderNumber", typeof(string));
  dt.Columns.Add("OrderItemSKUName", typeof(string));
  dt.Columns.Add("SKUNumber", typeof(string));
  dt.Columns.Add("OrderItemStatus", typeof(string));
  dt.Columns.Add("OrderItemUnitCount", typeof(string));
  dt.Columns.Add("mtrx_Code2", typeof(string));

  for (int i = 0; i &lt; gvOrderItems.Rows.Count; i++)
  {
    DataRow drNew = dt.NewRow();
    drNew["OrderDate"] = gvOrderItems.Rows[i].Cells[0].Text;
    drNew["OrderNumber"] = gvOrderItems.Rows[i].Cells[1].Text.Replace("&amp;nbsp;", "");
    drNew["OrderItemSKUName"] = gvOrderItems.Rows[i].Cells[3].Text;
    drNew["SKUNumber"] = gvOrderItems.Rows[i].Cells[2].Text;
    drNew["OrderItemStatus"] = gvOrderItems.Rows[i].Cells[6].Text;
    drNew["OrderItemUnitCount"] = gvOrderItems.Rows[i].Cells[5].Text;
    drNew["mtrx_Code2"] = gvOrderItems.Rows[i].Cells[4].Text;
    dt.Rows.Add(drNew);
  }

  if (dt != null)
  {
    DataView dataView = new DataView(dt);
    dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

    gvOrderItems.DataSource = dataView;
    gvOrderItems.DataBind();
  }
}
</code></pre>

<p><code>dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);</code> e.SortDirection always shows as Ascending?</p>

<pre><code>&lt;asp:GridView ID="gvOrderItems" runat="server" GridLines="None" CellSpacing="-1" AutoGenerateColumns="false" 
  AllowSorting="true" OnSorting="gvOrderItems_Sorting"&gt;
  &lt;Columns&gt;                             
    &lt;asp:BoundField DataField="OrderDate" HeaderText="Date" SortExpression="OrderDate" /&gt;
    &lt;asp:BoundField DataField="OrderNumber" HeaderText="Order Number" SortExpression="OrderNumber" /&gt;
    &lt;asp:BoundField DataField="SKUNumber" HeaderText="Product Number" SortExpression="OrderNumber" /&gt;
    &lt;asp:BoundField DataField="OrderItemSKUName" HeaderText="Product Description" /&gt;
    &lt;asp:BoundField DataField="mtrx_Code2" HeaderText="Size" /&gt;
    &lt;asp:BoundField DataField="OrderItemUnitCount" HeaderText="OTY" /&gt;
    &lt;asp:BoundField DataField="OrderItemStatus" HeaderText="Status" /&gt;        
  &lt;/Columns&gt;
&lt;/asp:GridView&gt;
</code></pre>

<p><strong>UPDATE</strong></p>

<p>OK after some advice from Kostrzak I looked up the SO question <a href="https://stackoverflow.com/questions/250037/gridview-sorting-sortdirection-always-ascending">GridView sorting: SortDirection always Ascending</a> and modified my Sorting event to this......</p>

<pre><code>protected void gvOrderItems_Sorting(object sender, GridViewSortEventArgs e)
{   

  DataTable dt = new DataTable();
  dt.Columns.Add("OrderDate", typeof(string));
  dt.Columns.Add("OrderNumber", typeof(string));
  dt.Columns.Add("OrderItemSKUName", typeof(string));
  dt.Columns.Add("SKUNumber", typeof(string));
  dt.Columns.Add("OrderItemStatus", typeof(string));
  dt.Columns.Add("OrderItemUnitCount", typeof(string));
  dt.Columns.Add("mtrx_Code2", typeof(string));

  for (int i = 0; i &lt; gvOrderItems.Rows.Count; i++)
  {
    DataRow drNew = dt.NewRow();
    drNew["OrderDate"] = gvOrderItems.Rows[i].Cells[0].Text;
    drNew["OrderNumber"] = gvOrderItems.Rows[i].Cells[1].Text.Replace("&amp;nbsp;", "");
    drNew["OrderItemSKUName"] = gvOrderItems.Rows[i].Cells[3].Text;
    drNew["SKUNumber"] = gvOrderItems.Rows[i].Cells[2].Text;
    drNew["OrderItemStatus"] = gvOrderItems.Rows[i].Cells[6].Text;
    drNew["OrderItemUnitCount"] = gvOrderItems.Rows[i].Cells[5].Text;
    drNew["mtrx_Code2"] = gvOrderItems.Rows[i].Cells[4].Text;
    dt.Rows.Add(drNew);
  }

  if (dt != null)
  {
    if (e.SortExpression == (string)ViewState["SortColumn"])
    {
      // We are resorting the same column, so flip the sort direction
      e.SortDirection =
          ((SortDirection)ViewState["SortColumnDirection"] == SortDirection.Ascending) ?
          SortDirection.Descending : SortDirection.Ascending;
    }
    // Apply the sort
    dt.DefaultView.Sort = e.SortExpression +
        (string)((e.SortDirection == SortDirection.Ascending) ? " ASC" : " DESC");
    ViewState["SortColumn"] = e.SortExpression;
    ViewState["SortColumnDirection"] = e.SortDirection;

    gvOrderItems.DataSource = dt;
    gvOrderItems.DataBind();
  }
}
</code></pre>

<p>On <code>Page Load</code> the Date order is in the Descending order. </p>

<p>2016-03-08<br />
2016-02-10<br />
2016-01-22<br />
2016-01-22<br />
2016-01-22<br />
2016-01-22<br />
2016-01-22<br />
2016-01-22<br />
2015-11-11<br />
2015-11-11<br /></p>

<p>After clicking on the Date column in the gridview it should go to Ascending order as follows:</p>

<p>2015-11-11<br />
2015-11-11<br />
2016-01-22<br />
2016-01-22<br />
2016-01-22<br />
2016-01-22<br />
2016-01-22<br />
2016-01-22<br />
2016-02-10<br />
2016-03-08<br /></p>

<p>However it is displayed in the following order </p>

<p>2016-03-08<br />
2016-02-10<br />
11/11/2015<br />
11/11/2015<br />
22/01/2016<br />
22/01/2016<br />
22/01/2016<br />
22/01/2016<br />
22/01/2016<br />
22/01/2016<br /></p>

<p>I am not really sure how it gets to this order??</p>