How to use DataPager with Database Paged

后端 未结 2 1959
长情又很酷
长情又很酷 2021-01-06 08:17

I am using ListView/DataPager.

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

At my C# code just comes one page at time. H

2条回答
  •  醉梦人生
    2021-01-06 09:11

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

    public class PagedList : IEnumerable, ICollection
    {
        private IEnumerable ActualPage { get; set; }
        private int Total { get; set; }
        private int StartIndex { get; set; }
    
        public PagedList(int total, int startIndex, IEnumerable actualPage)
        {
            ActualPage = actualPage;
            Total = total;
            StartIndex = startIndex;
        }
    
        public IEnumerator 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
    }
    

    Usage example:

    int totalRows = DB.GetTotalPeople();
    int rowIndex = (currentPage-1)*pageSize;
    List peoplePage = DB.GetPeopleAtPage(currentPage);
    
    listview.DataSource = new PagedList(totalRows, rowIndex, peoplePage)
    listView.DataBind();
    

提交回复
热议问题