Saturday, 28 June 2008

How do I calculate the maximum page index when paging in C#.NET?

using System;

public static class PagingUtility
{
/// <summary>
/// Calculates the maximum page index
/// </summary>
/// <param name="itemCount">Number of items to be paged</param>
/// <param name="pageSize">Number of items on each page</param>
/// <returns>Maximum page index (>=1)</returns>
public static int MaximumPageIndex( int itemCount, int pageSize )
{
int returnValue = 1;

if ( itemCount > 0 && itemCount > pageSize && pageSize > 0 )
{
returnValue = itemCount / pageSize;

int remainder = itemCount % pageSize;

if ( remainder > 0 )
{
returnValue++;
}
}

return returnValue;
}
}

1 comment:

Anonymous said...

Very nicce!