1. 程式人生 > >C# DataTable分頁處理

C# DataTable分頁處理

for toa cast array urn int data [] edt

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

C# DataTable分頁處理