How to use DataPager with a Paged Database

I am using ListView / DataPager.

For performance reasons, I publish my results in the database using ROW_NUMBER (SQl2005).

In my C # code, one page just comes at a time. How can I tell DataPager that I have more rows that are really in my list?

+4
source share
2 answers

I created a class that gerenate fake default objects (T). Works great:

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 } 

Usage example:

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

Apparently, I cannot comment on the above solution provided by Fujiy, however I found the following error:

Inside GetEnumerator (), incrementing in the else branch always causes the collection to skip one default item if you are not on the last page of the PagedList.

As an example, if you create a computed list of 5 items, with a starting index of 3 and 1 per page. This could introduce an else branch for element 2. It will increase i to 3, and then return to the for header, where it will increase to 4, without creating a default element for i == 3.

  • i == 1 -> default
  • i == 2 -> default
  • i == 3 -> Actual item
  • i == 4 β†’ Missed
  • i == 5 -> default

A simple solution is to either use 3 for-loops (one for default values ​​before ActualPage, one for ActualPage and one for items after ActualPage). Or add me - after the For loop inside the Else branch.

0
source

Source: https://habr.com/ru/post/1304142/