0

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?

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

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

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 < 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("&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();
  }
}

dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection); e.SortDirection always shows as Ascending?

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

UPDATE

OK after some advice from Kostrzak I looked up the SO question GridView sorting: SortDirection always Ascending and modified my Sorting event to this......

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 < 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("&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();
  }
}

On Page Load the Date order is in the Descending order.

2016-03-08
2016-02-10
2016-01-22
2016-01-22
2016-01-22
2016-01-22
2016-01-22
2016-01-22
2015-11-11
2015-11-11

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

2015-11-11
2015-11-11
2016-01-22
2016-01-22
2016-01-22
2016-01-22
2016-01-22
2016-01-22
2016-02-10
2016-03-08

However it is displayed in the following order

2016-03-08
2016-02-10
11/11/2015
11/11/2015
22/01/2016
22/01/2016
22/01/2016
22/01/2016
22/01/2016
22/01/2016

I am not really sure how it gets to this order??

Community
  • 1
  • 1
KJSR
  • 1,679
  • 6
  • 28
  • 51
  • 2
    There was already similar issue on SO: http://stackoverflow.com/questions/250037/gridview-sorting-sortdirection-always-ascending – Kostrzak Apr 10 '16 at 19:46
  • Thanks, Kostrzak I have updated the question. – KJSR Apr 10 '16 at 20:32
  • 1
    Do you need to refill the DataTable from the GridView in your Sorting event handler? Calling `PopulateOrderList`wouldn't do the same? Another point: sorting would be more predictable if the date was in a Date field instead of a string. Maybe calling `PopulateOrderList` would also solve that problem. – ConnorsFan Apr 10 '16 at 21:08
  • The 'PopulateOrderList' gets the data in a datatable and then adds it to the pds (PagedDataSource) method. However accessing the current Data from gridview shouldn't be a problem? I am not sure on your last point? – KJSR Apr 10 '16 at 23:55
  • 1
    It just appears strange to me to reload the data from the GridView instead of getting it from the source. As for my last point, I thought that if you got the data from the source, `OrderDate` would be a Date field, not a string field, and that would help to Sort the data correctly. – ConnorsFan Apr 11 '16 at 00:38

1 Answers1

0

Found the problem. As ConnorsFan pointed it out the OrderDate field was being set as a String instead of a DateTime field. So I changed the field datatype from typeof(string) to typeof(datetime) when I created the datatable and this resolved the issue

Before

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));

After

DataTable dt = new DataTable();
dt.Columns.Add("OrderDate", typeof(DateTime));
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));
KJSR
  • 1,679
  • 6
  • 28
  • 51