/// <summary>
/// DataTable分页方法,和其他的方法比较优势为不需要DataTable.Copy()操作,
/// 节约了一定的资源。By Adchun
/// </summary>
/// <param name="dataTable"></param>
/// <param name="curPageNo"></param>
/// <param name="pageSize"></param>
/// <param name="rowCount"></param>
/// <param name="maxPageNo"></param>
/// <param name="prePageNo"></param>
/// <param name="nextPageNo"></param>
public static void SetTablePage
(
ref DataTable dataTable, //要分页的表
ref int curPageNo, //当前页码,从1开始
int pageSize, //每页记录数
ref int rowCount, //返回记录总数
ref int maxPageNo, //返回最大页码
ref int prePageNo, //返回当前页的上一页码
ref int nextPageNo //返回当前页的下一页码
)
{
rowCount = dataTable.Rows.Count;
maxPageNo = rowCount % pageSize == 0 ? rowCount / pageSize : rowCount / pageSize + 1;
//以下这句为防止curPageNo出界,可有可无
curPageNo = curPageNo < 1 ? curPageNo = 1 : curPageNo > maxPageNo ? curPageNo = maxPageNo : curPageNo;
prePageNo = curPageNo - 1 == 0 ? curPageNo : curPageNo - 1;
nextPageNo = curPageNo + 1 > maxPageNo ? maxPageNo : curPageNo + 1;
//从上往下删除表中不需要的记录
int cycTimes = (curPageNo - 1) * pageSize;
for (int i = 0; (dataTable.Rows.Count > 0 && i < cycTimes); i++)
{
dataTable.Rows.RemoveAt(0);
}
//从下往上删除表中不需要的记录,i值减少的同时也意味着Rows.Count值的减少
for (int i = dataTable.Rows.Count; i > pageSize; i--)
{
dataTable.Rows.RemoveAt(i - 1);
}
}
Asp.Net DataTable 简单分页(新)
最新推荐文章于 2020-06-03 22:08:36 发布