Gridview:捕获排序方向

发布于 2024-10-15 13:25:32 字数 337 浏览 4 评论 0原文

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

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

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

为什么它不能从升序切换到降序并返回?

谢谢。

I have a gridview in an updatepanel with sorting enabled and an event handler as follows:

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

I put a breakpoint just after these lines. Every time I press the column header, my variable TheDirection is always showing Ascending.

Why is it not toggling from ascending to descending and back?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丶视觉 2024-10-22 13:25:32

我一直在阅读,当您手动向 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();
}

希望它有帮助

I've been reading and the sorting seems to break when you are manually providing the gridview a datasource. Not sure if that is your case, but this works for me..

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

hope it helps

溺深海 2024-10-22 13:25:32

您可以将方向保留在 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;

}

You could keep the direction in the ViewState or the Session. Like this (Untested Code):

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;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文