C# DataTable分页处理

public DataTable GetPagedTable(DataTable dt, int PageIndex, int PageSize)//PageIndex表示第几页,PageSize表示每页的记录数
{
if (PageIndex == 0)
return dt;//0页代表每页数据,直接返回

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;
}

 

//第二种方式


public DataRow[] GetTableRows(DataTable dtAllEas, int PageIndex,int PageSize)
{
  var rows = dtAllEas.Rows.Cast<DataRow>();
  var curRows = rows.Skip(PageIndex).Take(PageSize).ToArray();  
}

posted @ 2017-08-03 18:36  左正  阅读(495)  评论(0编辑  收藏  举报