方法一:
public static DataTable DtPage(DataTable dt, int pageIndex, int pageSize)
{
DataTable pagedt = dt.Clone();
var query = dt.AsEnumerable().Skip(pageIndex).Take(pageSize);
foreach (DataRow item in query)
{
pagedt.Rows.Add(item.ItemArray);
}
return pagedt;
}
方法二:
public static DataTable DtPage(DataTable dt, int pageIndex, int pageSize)
{
if (pageIndex== 0) { return dt; }
DataTable newdt = dt.Copy();
newdt.Clear();
int rowbegin = (pageIndex- 1) * pageSize;
int rowend = pageIndex * pageSize;
if (rowbegin >= dt.Rows.Count) { return newdt; }
if (rowend > dt.Rows.Count) { rowend = dt.Rows.Count; }
for (int i = rowbegin; i <= rowend - 1; i++)
{
DataRow newdr = newdt.NewRow();
DataRow dr = dt.Rows[i];
foreach (DataColumn column in dt.Columns)
{
newdr[column.ColumnName] = dr[column.ColumnName];
}
newdt.Rows.Add(newdr);
}
return newdt;
}
返回分页后的总页数:
public int getTotalPage(int totalCount, int pageSize)
{
var totalPage = (totalCount / pageSize) + (totalCount % pageSize > 0 ? 1 : 0);
return totalPage;
}