码迷,mamicode.com
首页 > 编程语言 > 详细

对datatable操作经验-排序和分页

时间:2016-09-02 20:24:36      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

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操作经验-排序和分页

标签:

原文地址:http://www.cnblogs.com/lvlaozf/p/5834546.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!