排序gridview的选择

排序gridview的选择

本文介绍了排序gridview的选择错了行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a gridview (actually a SPgridview)

和我所做的COLUMNNAME可点击使用户可使用的数据行进行排序。
这工作得很好。

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

当用户尝试选择行他们排序的数据后,会出现问题。
我可以看到,在GridView还挺行是如何分类和选择,这是该指数点击它得到了排序前行忘记。

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..

我要如何解决呢?
我试图再次排序行的用户选择一行之后,但似乎不工作。
并应在GridView记住,这只是排序的事实呢?

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?

在此先感谢:)

推荐答案

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

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

这篇关于排序gridview的选择错了行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 06:05