DataTable 数据分页
/// <summary>
/// DataTable 数据分页
/// </summary>
/// <param name="dt">数据源</param>
/// <param name="PageIndex">第几页</param>
/// <param name="PageSize">每页多少条</param>
/// <returns></returns>
public static DataTable getPageToDataTable(DataTable dt, int PageIndex, int PageSize)
{
if (PageIndex == 0)
return dt;//0页代表每页数据,直接返回
if (dt == null)
{
DataTable table = new DataTable();
return table;
}
DataTable newdt = dt.Copy();
newdt.Clear();//copy dt的框架
int rowbegin = (PageIndex - 1) * PageSize;
int rowend = PageIndex * PageSize;//要展示的数据条数
if (rowbegin >= dt.Rows.Count)
return newdt;//源数据记录数小于等于要显示的记录,直接返回dt
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;
}