1

我在更新面板中有一个启用排序的网格视图和一个事件处理程序,如下所示:

protected void MyGridSort(object sender, GridViewSortEventArgs e)
{
   var TheDirection = (e.SortDirection).ToString();
   var TheColumn = (e.SortExpression).ToString();
}

我在这些行之后放置了一个断点。每次按列标题时,我的变量 TheDirection 总是显示升序。

为什么它不从上升到下降再返回?

谢谢。

4

2 回答 2

2

我一直在阅读,当您手动为 gridview 提供数据源时,排序似乎中断了。不确定这是否是你的情况,但这对我有用..

string strSortExpression = e.SortExpression + " ASC";
if (Convert.ToString(ViewState["SortExpression"]) == strSortExpression)
{
    strSortExpression = e.SortExpression + " DESC";
}
ViewState["SortExpression"] = strSortExpression;


//This is done by sorting the Default View of the underlying data and then re-binding this
//to the grid.
System.Data.DataTable myData = HttpContext.Current.Session["GridData"] as System.Data.DataTable;
if (myData != null)
{
   myData.DefaultView.Sort = strSortExpression;
   GridView1.DataSource = myData;
   GridView1.DataBind();
}

希望能帮助到你

于 2011-06-01T19:03:40.503 回答
0

您可以将方向保留在 ViewState 或 Session 中。像这样(未经测试的代码):

protected void MyGridSort(object sender, GridViewSortEventArgs e)
{
   var TheDirection = (e.SortDirection).ToString();
   var TheColumn = (e.SortExpression).ToString();

   string prevColumn = "", prevDirection = "";

   if (Session["MyGridSortColumn"] != null)
      prevColumn = Session["MyGridSortColumn"].ToString();
   if (Session["MyGridSortDirection"] != null)
      prevDirection = Session["MyGridSortDirection"].ToString();

   if (TheColumn == prevColumn) {
      if (prevDirection == "ASC")
         TheDirection = "DESC";
      else
         TheDirection = "ASC";
   }

   Session["MyGridSortDirection"] = TheDirection;
   Session["MyGridSortColumn"] = TheColumn;

}
于 2011-02-02T03:04:16.680 回答