排序的gridview选择错误的行

发布于 2024-07-20 15:23:37 字数 271 浏览 8 评论 0原文

我有一个 gridview (实际上是 SPgridview )

,我使列名可单击,以便用户可以使用数据对行进行排序。 效果很好。

当用户在对数据进行排序后尝试选择一行时,就会出现此问题。 我可以看到 gridview 有点“忘记”行是如何排序的,并在排序之前选择了单击索引处的行。

我该如何解决这个问题? 我尝试在用户选择一行后再次对行进行排序,但这似乎不起作用。 gridview 应该记住它刚刚排序的事实吗?

提前致谢 :)

I have a gridview (actually a SPgridview)

And i made the columnname clickable so the users can sort the rows using the data.
And that works fine.

The problem occurs when users try to select a row after they sorted the data.
I can see that the gridview kinda "forgets" how the rows were sorted and selects the row that was at the clicked index before it got sorted..

How do i fix that?
I tried sorting the row again after the user selects a row, but that doesnt seem to work.
And should the gridview remember the fact that it was just sorted?

Thanks in advance :)

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

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

发布评论

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

评论(6

假面具 2024-07-27 15:23:37

当您处理排序事件时,设置一个会话变量,将其设置为排序方向,并在重新绑定数据源时使用它。

protected void gridview_Sorting()
{
    // BIND DATA Function
    BindData();

    DataTable dt = gridview.DataSource as DataTable;

    if (dt != null)
    {
        //Sort the data.
        dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);

        Session["sort"] = dt.DefaultView.Sort;
        gridview.DataSource = dt;
        gridview.DataBind();
    }
}





// bind data function//

private void BindData()
{

    DataTable dt = GetDataTable();

    if (Session["sort"] != null)
    {
        //Sort the data.
        dt.DefaultView.Sort = Session["sort"].ToString();
    }

    gridview.DataSource = dt;
    gridview.DataBind();
}

When you handle the Sorting event set a session variable set it to the sort direction and use it when you rebind your datasource.

protected void gridview_Sorting()
{
    // BIND DATA Function
    BindData();

    DataTable dt = gridview.DataSource as DataTable;

    if (dt != null)
    {
        //Sort the data.
        dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);

        Session["sort"] = dt.DefaultView.Sort;
        gridview.DataSource = dt;
        gridview.DataBind();
    }
}





// bind data function//

private void BindData()
{

    DataTable dt = GetDataTable();

    if (Session["sort"] != null)
    {
        //Sort the data.
        dt.DefaultView.Sort = Session["sort"].ToString();
    }

    gridview.DataSource = dt;
    gridview.DataBind();
}
在巴黎塔顶看东京樱花 2024-07-27 15:23:37

确保回发后没有重新绑定网格。

if(!IsPostBack)
{
  gridView.DataSource = yourDataSource;
  gridView.DataBind();
}

Make sure that you are not rebinding the grid after a postback.

if(!IsPostBack)
{
  gridView.DataSource = yourDataSource;
  gridView.DataBind();
}
甜柠檬 2024-07-27 15:23:37

您是通过行索引还是通过要编辑的数据的唯一标识符来获取所选行? 如果您通过行索引获取,则可能会“忘记”,因为您正在 OnPostBack 上重新创建网格。 尝试迭代数据并通过其唯一 ID(而不是行索引)选择它。

Are you grabbing the selected row by it's row index or by the unique identifier of the data you are wanting to edit? If you're getting by row index, it may be 'forgetting' since you are recreating the Grid on OnPostBack. Try iterating through the data and select it by it's unique ID, not its row index.

や莫失莫忘 2024-07-27 15:23:37

查看 Johans 博客中有关 SPGridView 和 LinqDataSource 的内容

Check Johans blog regarding SPGridView and LinqDataSource

美胚控场 2024-07-27 15:23:37

我制作了许多可排序的 GridView,但在今天之前,当我偶然发现这个问题时,没有一个具有行命令交互。 我的是一个“普通”GridView,而不是 SPgridview。 我发现这是可行的:

  1. 在bindData()中,如果我们还没有创建DataTable并将其放入Session对象中,那么就这样做。 否则,我们将使用现有的、已排序的 DataTable:
if (Session["dtbl"] == null) { 
    会话[“dtbl”] = method_to_fetch_datatable(); 
  } 
  gv.DataSource = Session["dtbl"] as DataTable; 
  gv.DataBind(); 
  
  1. 在 GridView 处理 INSERT、UPDATE 或 DELETE 基础数据的行命令时,刷新 Session 对象,维护排序(如果有):
Session["dtbl"] = method_to_fetch_datatable(); 
  if (ViewState["SortExpression"] != null) { 
    DataTable dt = Session["dtbl"] as DataTable; 
    dt.DefaultView.Sort = ViewState["SortExpression"] 作为字符串; 
  } 
  绑定数据(); 
  

I'd made a number of sortable GridViews, but none with row command interaction before today, when I stumbled on this problem. Mine is a "plain" GridView, not SPgridview. I found this works:

  1. In bindData(), if we have not created a DataTable and put it in the Session object, do so. Otherwise, we'll use the existing, sorted DataTable:
if (Session["dtbl"] == null) {
  Session["dtbl"] = method_to_fetch_datatable();
}
gv.DataSource = Session["dtbl"] as DataTable;
gv.DataBind();
  1. In the GridView's handling of row commands that INSERT, UPDATE or DELETE underlying data, refresh the Session object, maintaining the sort, if there is one:
Session["dtbl"] = method_to_fetch_datatable();
if (ViewState["SortExpression"] != null) {
  DataTable dt = Session["dtbl"] as DataTable;
  dt.DefaultView.Sort = ViewState["SortExpression"] as string;
}
bindData();
乞讨 2024-07-27 15:23:37

我成功了。 (有点)

在排序事件中,我保存了排序表达式(用于排序的列的名称)
以及排序方向升序或降序。

然后我为 gridview 和 databind 创建数据源,在数据绑定之后,我使用 gridview.sort 命令按我保存在 viewstate 中的值进行排序。

效果很好,只有一个问题。
排序时,我在多次按下同一列后使其切换方向。
现在它认为我一直按列标题,所以它不断反转排序。

但我很克制地让它只朝一个方向排序。
现在我在排序事件中处理发送者对象,我想如果我可以获得一些有关导致事件的原因的信息,我可以告诉它仅根据发送者切换方向。

谢谢 :)

I got it working. (kinda)

In the sorting event i saved the sortexpression (the name of the column used to sort by)
and the sortdirection ascending or descending.

Then i make the datasource for the gridview and databind, and after databinding it, i use the gridview.sort command to sort by the values i saved in viewstate.

That works fine, only one problem.
When sorting i made it switch direction after pressing the same column more than one time.
Now it thinks i keep pressing the column title, so it keeps reversing the sorting.

But i tempererarely made it only sort in one direction.
And now im playing with the sender object in the sorting event, im thinking that if i could get some info about whats causing the event i could tell it to only switch direction based on the sender.

Thanks :)

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