DataView丢失排序顺序

时间:2016-04-06 14:37:49

标签: c# sorting

我对DataView.Sort有点疑问。当它绑定到pageddatasource时会丢失排序顺序吗?

默认情况下,数据按日期降序排序。

这是我的代码

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

}

当数据从数据库返回到DataTable dts时,排序很好。但是当使用DataView对它进行排序时,它似乎失去了排序顺序?

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

我不确定究竟是怎么回事?

0 个答案:

没有答案