If all of your data will fit in available memory, you could retrieve all of the data you need to display, and then extract only the items that will fit in a page. Psuedo code:
0) retrieve data
1) Calculate number of pages you have -
Math.Min(1, Math.Floor(records_retrieved / records_per_page)))
2) Extract the items. I'd probably write an extension method to do this, and then you can use it on any apprpopriately typed collection. The guts of the method would look something like this:
List<T> result = new List<T>();
int skipItems = recsPerPage * (pageNumber - 1);
if (list.Count > skipItems)
{
list.Skip(skipItems);
result = list.Take(Math.Min(list.Count - skipItems, 100);
}
return result;
EDIT =====================
Here's some code that works:
List<string> myStrings = new List<string>();
myStrings.Add("1");
myStrings.Add("2");
myStrings.Add("3");
myStrings.Add("4");
myStrings.Add("5");
List<string> result = myStrings.GetPageOfData(3, 1);</string>
Here's the extension method:
public static List<T> GetPageOfData<T>(this List<T> list, int pageNumber, int recsPerPage)
{
List<T> results = new List<T>();
int skipItems = recsPerPage * (pageNumber - 1);
int itemsToReturn = Math.Min(list.Count - skipItems, recsPerPage) - 1;
if (list.Count > skipItems)
{
for (int i = skipItems; i <= (skipItems + itemsToReturn); i++)
{
results.Add(list[i]);
}
}
return results;
}