如何使用DataPager的使用数据库分页 [英] How to use DataPager with Database Paged

查看:81
本文介绍了如何使用DataPager的使用数据库分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的ListView / DataPager的。

I am using ListView/DataPager.

有关性能方面的原因我页我的结果在数据库,使用ROW_NUMBER(SQL2005)。

For performance reasons I page my results at database, using ROW_NUMBER(SQl2005).

在我的C#code在时间刚刚来到一个页面。我怎样才能到DataPager的说,我有更多的行真的是我的列表?

At my C# code just comes one page at time. How can I say to DataPager that I have more rows that really are at my List?

推荐答案

我创建的gerenate假默认(T)对象的类。正常工作:

I created a class that gerenate fake default(T) objects. Worked fine:

public class PagedList<T> : IEnumerable<T>, ICollection
{
    private IEnumerable<T> ActualPage { get; set; }
    private int Total { get; set; }
    private int StartIndex { get; set; }

    public PagedList(int total, int startIndex, IEnumerable<T> actualPage)
    {
        ActualPage = actualPage;
        Total = total;
        StartIndex = startIndex;
    }

    public IEnumerator<T> GetEnumerator()
    {
        bool passouPagina = false;
        for (int i = 0; i < Total; i++)
        {
            if (i < StartIndex || passouPagina)
            {
                yield return default(T);
            }
            else
            {
                passouPagina = true;
                foreach (T itempagina in ActualPage)
                {
                    i++;
                    yield return itempagina;
                }
            }
        }
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    #region Implementation of ICollection

    void ICollection.CopyTo(Array array, int index)
    {
        throw new NotSupportedException();
    }

    public int Count
    {
        get { return Total; }
    }

    object ICollection.SyncRoot
    {
        get { throw new NotSupportedException(); }
    }

    bool ICollection.IsSynchronized
    {
        get { throw new NotSupportedException(); }
    }

    #endregion
}

用法示例:

int totalRows = DB.GetTotalPeople();
int rowIndex = (currentPage-1)*pageSize;
List<Person> peoplePage = DB.GetPeopleAtPage(currentPage);

listview.DataSource = new PagedList(totalRows, rowIndex, peoplePage)
listView.DataBind();

这篇关于如何使用DataPager的使用数据库分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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