对datatable操作经验-排序和分页
Posted laolv
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对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操作经验-排序和分页的主要内容,如果未能解决你的问题,请参考以下文章