如何将DataPager与数据库页面一起使用

Fel*_*oto 4 c# asp.net

我正在使用ListView/DataPager.

出于性能原因,我使用ROW_NUMBER(SQl2005)将结果分页到数据库.

在我的C#代码中,一次只有一页.我怎么能对DataPager说我有更多的行真的在我的列表?

Fel*_*oto 5

我创建了一个创建假默认(T)对象的类.工作得很好:

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
}
Run Code Online (Sandbox Code Playgroud)

用法示例:

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

listview.DataSource = new PagedList(totalRows, rowIndex, peoplePage)
listView.DataBind();
Run Code Online (Sandbox Code Playgroud)