首页 > 代码库 > 对datatable操作经验-排序和分页
对datatable操作经验-排序和分页
1.datatable排序1:
public DataTable SortDesc(DataTable dt){
DataView dv = new DataView();
dv.Table = dt;
dv.Sort = "autoid desc";
return dv.ToTable();
}
2、datatable排序2
public DataTable SortDesc(DataTable dt){
dt.DefaultView.Sort =
"ID ,Name ASC"
;
dt=dt.DefaultView.ToTable();
return dt;
}
3、对datatable进行分页
/// 根据索引和pagesize返回记录
/// </summary>
/// <param name="dt">记录集 DataTable</param>
/// <param name="PageIndex">当前页</param>
/// <param name="pagesize">一页的记录数</param>
/// <returns></returns>
public static DataTable SplitDataTable(DataTable dt, int PageIndex, int PageSize)
{
if (PageIndex == 0)
return dt;
DataTable newdt = dt.Clone();
//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;
}
对datatable操作经验-排序和分页