GridView排序&分页 [英] GridView sorting & paging

查看:17
本文介绍了GridView排序&分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的页面上显示简单的 gridview 并为其提供排序和分页功能.单独排序和分页工作正常,但两者的组合不正常.例如,如果我对第一列进行降序排序,然后转到第二页,那么我会看到第二页数据的默认排序(升序).

I would like to display simple gridview on my page and provide sorting and paging functionality to it. Sorting and paging individually is working ok, but the combination of both does not. For example if I sort the first column descending and then go to page number two, then I see the second page of data with default sorting (ascending).

我非常依赖这个问题的代码:GridView 排序:SortDirection always Ascending,但问题依然存在.另外 - 似乎我必须更改我的代码,以便使用 Session 对象而不是 ViewState,还必须弄清楚这一点......

I relied heavily on the code from this question: GridView sorting: SortDirection always Ascending, but the problem remains. Also - as it seems I have to change my code so that instead of Session object ViewState is used, have to figure this out also...

我的代码,简化了,只有两列:

My code, simplified, with only two columns:

aspx:

<asp:GridView ID="dgvView" runat="server" AutoGenerateColumns="false" AllowPaging="true"
    PageSize="10" AllowSorting="True" OnPageIndexChanging="DgvViewPageIndexChanging" OnSorting="OnSort">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name">
            <ItemStyle />
        </asp:BoundField>
        <asp:BoundField DataField="BirthDate" HeaderText="Birth date" DataFormatString="{0:dd.MM.yyyy}"
            SortExpression="BirthDate">
            <ItemStyle />
        </asp:BoundField>
    </Columns>
</asp:GridView>

和代码隐藏:

public partial class TestPage :Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DisplayData();
        }
    }

    private void DisplayData()
    {
        Session["TableView"] = GetUsers();
        dgvView.DataSource = Session["TableView"];
        dgvView.DataBind();
    }

    protected void DgvViewPageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        dgvView.PageIndex = e.NewPageIndex;
        DisplayData();
    }

    private List<MyUser> GetUsers()
    {
        var users = new List<MyUser>();
        for (int i = 0; i < 100; i++)
        {
            users.Add(new MyUser("Name" + i.ToString().PadLeft(2, '0'), new DateTime(2000, 1, 1).AddDays(i)));
        }
        return users;
    }

    private class MyUser
    {
        public string Name { get; private set; }
        public DateTime BirthDate { get; private set; }

        public MyUser(string name, DateTime birthDate)
        {
            Name = name;
            BirthDate = birthDate;
        }
    }

    protected void OnSort(object sender, GridViewSortEventArgs e)
    {
        Func<MyUser, object> f;
        if (e.SortExpression == "Name") f = u => u.Name;
        else f = u => u.BirthDate;

        dgvView.DataSource = Sort((List<MyUser>)Session["TableView"], f, GetSortDirection(e.SortExpression));
        dgvView.DataBind();
    }

    private List<MyUser> Sort<T>(IEnumerable<MyUser> user, Func<MyUser, T> f, SortDirection sortDirection)
    {
        if (sortDirection == SortDirection.Ascending) return user.OrderBy(f).ToList();
        return user.OrderByDescending(f).ToList();
    }

    private SortDirection GetSortDirection(string column)
    {
        string sessionVariable = "TableSort" + column;
        SortDirection sortDirection;

        if (Session[sessionVariable] == null)
        {
            sortDirection = SortDirection.Ascending;
        }
        else if ((SortDirection)Session[sessionVariable] == SortDirection.Ascending)
        {
            sortDirection = SortDirection.Descending;
        }
        else
        {
            sortDirection = SortDirection.Ascending;
        }

        Session[sessionVariable] = sortDirection;
        return sortDirection;
    }
}

推荐答案

我在另外两个答案的帮助下解决了.

I solved it with the help of other two answers.

我的解决方案:

public partial class TestPage1 : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["Data"] = MyUser.GetUsers();
            ViewState["SortExpression"] = "Name";
            DisplayData("Name", SortDirection.Ascending);
        }
    }

    private void DisplayData(string sortExpression, SortDirection sortDirection)
    {
        Func<MyUser, object> f;
        if (sortExpression == "Name") f = u => u.Name;
        else f = u => u.BirthDate;

        if (sortDirection == SortDirection.Ascending)
        {
            dgvView.DataSource = ((IEnumerable<MyUser>)ViewState["Data"]).OrderBy(f).ToList();
        }
        else
        {
            dgvView.DataSource = ((IEnumerable<MyUser>)ViewState["Data"]).OrderByDescending(f).ToList();
        }

        dgvView.DataBind();
    }

    protected void DgvViewPageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        dgvView.PageIndex = e.NewPageIndex;

        string sortExpression = ViewState["SortExpression"].ToString();
        DisplayData(sortExpression, GetDefaultSortDirection(sortExpression));
    }

    protected void OnSort(object sender, GridViewSortEventArgs e)
    {
        ViewState["SortExpression"] = e.SortExpression;
        ViewState[e.SortExpression] = GetReverseSortDirection(e.SortExpression);
        DisplayData(e.SortExpression, (SortDirection)ViewState[e.SortExpression]);
    }

    private SortDirection GetDefaultSortDirection(string sortExpression)
    {
        if (ViewState[sortExpression] == null)
        {
            ViewState[sortExpression] = SortDirection.Ascending;
        }

        return (SortDirection)ViewState[sortExpression];
    }

    private SortDirection GetReverseSortDirection(string sortExpression)
    {
        if (ViewState[sortExpression] == null)
        {
            ViewState[sortExpression] = SortDirection.Descending;
        }
        else
        {
            ViewState[sortExpression] = (SortDirection) ViewState[sortExpression] == SortDirection.Descending ? SortDirection.Ascending : SortDirection.Descending;
        }

        return (SortDirection)ViewState[sortExpression];
    }
}

这篇关于GridView排序&amp;分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆