Sorted gridview selects wrong row

别说谁变了你拦得住时间么 提交于 2019-12-22 12:45:44

问题


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 :)


回答1:


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

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



回答2:


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



回答3:


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.




回答4:


Check Johans blog regarding SPGridView and LinqDataSource




回答5:


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



回答6:


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 :)



来源:https://stackoverflow.com/questions/870344/sorted-gridview-selects-wrong-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!