简体   繁体   English

ASP.Net即使进入下一页也要对Gridview进行排序

[英]ASP.Net Sorting a Gridview even after going to next page

Hi all I am having trouble with the sorting of my gridview. 大家好,我在排序gridview时遇到了麻烦。 Let's say my gridview has 10 rows of data and 2 columns, Name and Age. 假设我的gridview有10行数据和2列,Name和Age。 Paging is set to 5 rows of data a page. 分页设置为每页5行数据。 When i click on Name, it is sorted correctly. 当我点击名称时,它被正确排序。 However when i click on the next page, it becomes "unsorted" again. 但是,当我点击下一页时,它再次变为“未排序”。 Help please! 请帮助! I have attached the event handlers below. 我在下面附上了事件处理程序。

    protected void SearchResultGridView_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (ViewState["SearchDS"] == null)
            Response.Redirect("PESearh.aspx");
        DataTable SearchDT = ((DataSet)ViewState["SearchDS"]).Tables[0];

        string sortExpression = e.SortExpression;

        if (ViewState["SearchSort"] != null && ViewState["SearchSort"].ToString().Length > 0)
        {
            if (!ViewState["SearchSort"].ToString().Contains(sortExpression))
                GridViewSortDirection = SortDirection.Descending;
        }
        else
        {
            GridViewSortDirection = SortDirection.Descending;
        }

        if (GridViewSortDirection == SortDirection.Ascending)
        {
            GridViewSortDirection = SortDirection.Descending;
            sortExpression += " DESC";
            SearchDT.DefaultView.Sort = sortExpression;
        }
        else
        {
            GridViewSortDirection = SortDirection.Ascending;
            sortExpression += " ASC";
            SearchDT.DefaultView.Sort = sortExpression;
        }
        ViewState["SearchSort"] = sortExpression;

        this.SearchResultGridView.DataSource = SearchDT;
        this.SearchResultGridView.DataBind();
    }

and for my pagechanging 以及我的分页

    protected void SearchResultGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        SearchResultGridView.DataSource = ViewState["SearchDS"];

        SearchResultGridView.PageIndex = e.NewPageIndex;
        SearchResultGridView.DataBind();
    }

I need help so that the previous sorting also sorts the next page. 我需要帮助,以便先前的排序也可以排序下一页。 Please help thanks! 请帮忙谢谢!

Note: I do not want to sort the data before binding because I want the user to see the data first and then decide what to bind. 注意:我不想在绑定之前对数据进行排序,因为我希望用户首先查看数据然后决定要绑定的内容。

You seem to be saving the sort expression in ViewState["SearchSort"] . 您似乎在ViewState["SearchSort"]保存排序表达式。 When you change the page index, the value should still remain in the ViewState variable, so you can just grab it and set it again with: 当您更改页面索引时,该值仍应保留在ViewState变量中,因此您可以抓住它并使用以下命令再次设置它:

SearchDT.DefaultView.Sort = ViewState["SearchSort"].ToString();

Before sorting the data though, may want to get the DataTable first like so: 在排序数据之前,可能想要首先获取DataTable

DataTable SearchDT = ((DataSet)ViewState["SearchDS"]).Tables[0];

Then, instead of assigning ViewState["SearchDS"] the DataSource of your GridView directly, you can use the new DataTable instead: 然后,您可以直接使用新的DataTable代替ViewState["SearchDS"] GridViewDataSource

SearchResultGridView.DataSource = SearchDT;

Together, you have something like this: 你有这样的东西:

protected void SearchResultGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    DataTable SearchDT = ((DataSet)ViewState["SearchDS"]).Tables[0];
    SearchDT.DefaultView.Sort = ViewState["SearchSort"].ToString();
    SearchResultGridView.DataSource = SearchDT;

    SearchResultGridView.PageIndex = e.NewPageIndex;
    SearchResultGridView.DataBind();
}

To be safe though, you may want to check to may sure ViewState["SearchSort"] exists first before assigning (if not, then you can give it a default value). 为了安全起见,您可能需要检查以确定ViewState["SearchSort"]在分配之前是否存在(如果没有,那么您可以给它一个默认值)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM