0 Replies - 562 Views - Last Post: 07 April 2016 - 01:31 AM

#1 kevin_911  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 130
  • Joined: 02-April 09

DataView losing Sort order

Posted 07 April 2016 - 01:31 AM

I am having a bit of issue with DataView.Sort. When it is binded to the pageddatasource it lose the sort order?

By default the data is sorted by Date in Descending order.

Here is my code

protected void Page_Load(object sender, EventArgs e)
{

  if (!IsPostBack)
  {
    ViewState["Column"] = "OrderDate";
    ViewState["Order"] = "ASC";

    PopulateProductClass();
    PopulateProduct(); 
  }
  PopulateOrderList();
 }

private void PopulateOrderList()
{
  DateTime d;
  DateTime d2;

  CustomerInfo ki = CustomerInfoProvider.GetCustomerInfoByUserID(CooneenHelper.GetUserImpersonisationID());
  int nKustomerID = ki.CustomerID;
  DataTable dts = new DataTable();
  dts.Columns.Add("OrderDate", typeof(string));
  dts.Columns.Add("OrderNumber", typeof(string));
  dts.Columns.Add("OrderItemSKUName", typeof(string));
  dts.Columns.Add("SKUNumber", typeof(string));
  dts.Columns.Add("OrderItemStatus", typeof(string));
  dts.Columns.Add("OrderItemUnitCount", typeof(string));

  QueryDataParameters qdp = new QueryDataParameters();
  qdp.Add("@CustomerID", nKustomerID);

  if (drpProductClass.HasControls())
  {
    if (drpProductClass.SelectedValue.ToString() != "0" || drpProductClass.SelectedValue.ToString() == null) { qdp.Add("@OrderItemWRClass", drpProductClass.SelectedItem.ToString()); }
    if (drpProduct.SelectedValue.ToString() != "0") { qdp.Add("@OrderItemSKUID", drpProduct.SelectedValue.ToString()); }
  }           

  if (txtStartDate.Text != "") { d = DateTime.Parse(txtStartDate.Text); qdp.Add("@OrderItemDateFrom", d.ToString("yyyy-MM-dd")); }
  if (txtEndDate.Text != "") { d2 = DateTime.Parse(txtEndDate.Text); qdp.Add("@OrderItemDateTo", d2.ToString("yyyy-MM-dd")); }

  DataSet ds = gc.ExecuteQuery("CN_GetOrderItemByCustID", qdp, QueryTypeEnum.StoredProcedure, true);
  foreach (DataRow dr in ds.Tables[0].Rows)
  {
    DataRow drNew = dts.NewRow();
    drNew["OrderDate"] = Validationhelper.GetDateTime(dr["OrderDate"], DateTime.Now).ToShortDateString();
    drNew["OrderNumber"] = dr["OrderNumber"].ToString();
    drNew["OrderItemSKUName"] = dr["OrderItemSKUName"].ToString();
    drNew["SKUNumber"] = dr["SKUNumber"].ToString();
    drNew["OrderItemStatus"] = dr["OrderItemStatus"].ToString();
    drNew["OrderItemUnitCount"] = dr["OrderItemUnitCount"].ToString();      
    dts.Rows.Add(drNew);
  }

  //Clear the TextBox
  litResults.Text = String.Empty;

  if (dts.Rows.Count == 1)
    litResults.Text = "" + dts.Rows.Count.ToString() + " Order Items";
  else
    litResults.Text = "" + dts.Rows.Count.ToString() + " Order Items";


  PagedDataSource pds = new PagedDataSource();
  DataView view = dts.DefaultView;
  view.Sort = ViewState["Column"].ToString() + " " + ViewState["Order"].ToString();

  pds.DataSource = view;


  //allow paging, set page size, and current page
  pds.AllowPaging = true;
  pds.PageSize = PerPage;
  pds.CurrentPageIndex = CurrentPage;

  //show # of current page in label
  if (pds.PageCount > 1) litResults.Text += " - Showing page " + (CurrentPage + 1).ToString() + " of " + pds.PageCount.ToString();

  //disable prev/next buttons on the first/last pages
  btnPrev.Enabled = !pds.IsFirstPage;
  btnNext.Enabled = !pds.IsLastPage;

  rprOrders.Visible = true;

  rprOrders.DataSource = pds;
  rprOrders.DataBind();

}



When the data is returned from the database into DataTable dts the sorting is fine. However when it is sorted using the DataView it seems to lose its sorting order?

PagedDataSource pds = new PagedDataSource();
DataView view = dts.DefaultView;
view.Sort = ViewState["Column"].ToString() + " " +     ViewState["Order"].ToString();
//Sorting order lost in view
pds.DataSource = view;



Also checked further and it seems

view.Sort = ViewState["Column"].ToString() + " " + ViewState["Order"].ToString(); 


is upsetting the order?

Both
ViewState["Order"]
and
ViewState["Column"]
have data inside them

Is This A Good Question/Topic? 0
  • +

Page 1 of 1