142 lines
2.5 KiB
C#
142 lines
2.5 KiB
C#
using System;
|
|
namespace SPSolutions
|
|
{
|
|
public class PagingCalculator
|
|
{
|
|
private int _numberOfRecords;
|
|
private int _pageSize;
|
|
private int _pageNumber;
|
|
public int NumberOfRecords
|
|
{
|
|
get
|
|
{
|
|
return this._numberOfRecords;
|
|
}
|
|
set
|
|
{
|
|
this._numberOfRecords = value;
|
|
}
|
|
}
|
|
public int PageSize
|
|
{
|
|
get
|
|
{
|
|
return this._pageSize;
|
|
}
|
|
set
|
|
{
|
|
this._pageSize = value;
|
|
}
|
|
}
|
|
public int PageNumber
|
|
{
|
|
get
|
|
{
|
|
return this._pageNumber;
|
|
}
|
|
set
|
|
{
|
|
this._pageNumber = value;
|
|
}
|
|
}
|
|
public int NumberOfPages
|
|
{
|
|
get
|
|
{
|
|
if (this.PageSize == 0 || this.NumberOfRecords == 0)
|
|
{
|
|
return 0;
|
|
}
|
|
return (this.NumberOfRecords + this.PageSize - 1) / this.PageSize;
|
|
}
|
|
}
|
|
public int PreviousPage
|
|
{
|
|
get
|
|
{
|
|
if (this.PageNumber <= 1)
|
|
{
|
|
return 1;
|
|
}
|
|
return this.PageNumber - 1;
|
|
}
|
|
}
|
|
public bool HasPreviousPage
|
|
{
|
|
get
|
|
{
|
|
return this.PageNumber > 1;
|
|
}
|
|
}
|
|
public int NextPage
|
|
{
|
|
get
|
|
{
|
|
if (this.PageNumber >= this.NumberOfPages)
|
|
{
|
|
return this.NumberOfPages;
|
|
}
|
|
return this.PageNumber + 1;
|
|
}
|
|
}
|
|
public bool HasNextPage
|
|
{
|
|
get
|
|
{
|
|
return this.PageNumber < this.NumberOfPages;
|
|
}
|
|
}
|
|
public int BeginningIndex
|
|
{
|
|
get
|
|
{
|
|
return this.PageSize * (this.PageNumber - 1);
|
|
}
|
|
}
|
|
public int EndingIndex
|
|
{
|
|
get
|
|
{
|
|
if (this.NumberOfRecords == 0)
|
|
{
|
|
return -1;
|
|
}
|
|
int num = this.BeginningIndex + this.PageSize - 1;
|
|
if (num >= this.NumberOfRecords - 1)
|
|
{
|
|
return this.NumberOfRecords - 1;
|
|
}
|
|
return num;
|
|
}
|
|
}
|
|
public PagingCalculator()
|
|
{
|
|
}
|
|
public PagingCalculator(int numberOfRecords, int pageSize) : this(numberOfRecords, pageSize, 1)
|
|
{
|
|
}
|
|
public PagingCalculator(int numberOfRecords, int pageSize, int pageNumber)
|
|
{
|
|
if (numberOfRecords < 0)
|
|
{
|
|
throw new ArgumentOutOfRangeException("numberOfRecords", numberOfRecords, "Number of records must be greater than zero");
|
|
}
|
|
if (pageSize < 0)
|
|
{
|
|
throw new ArgumentOutOfRangeException("pageSize", pageSize, "Page size must be greater than zero");
|
|
}
|
|
if (pageNumber < 0)
|
|
{
|
|
throw new ArgumentOutOfRangeException("pageNumber", pageNumber, "Page number must be greater than zero");
|
|
}
|
|
this._numberOfRecords = numberOfRecords;
|
|
this._pageSize = pageSize;
|
|
this._pageNumber = pageNumber;
|
|
if (pageNumber > this.NumberOfPages && this.NumberOfPages != 0)
|
|
{
|
|
throw new ArgumentOutOfRangeException("pageNumber", pageNumber, "The page number is bigger than the number of pages.");
|
|
}
|
|
}
|
|
}
|
|
}
|