DataTable中数据记录的排序、检索、合并、分页、统计

一、排序

DataView dv=dt.DefaultView;

dv.Sort="id asc,name desc";

dt=dv.ToTable(); 

二、检索 

DataRow[] matches = dt.Select("(id<'003') and (name='名字11') and (number like '%2007%')");

string strName = matches[0]["name"].ToString(); 

三、合并 

假如有2个DataTabel:Dt1,Dt2。表结构一样将Dt2接在Dt1后可采用方法 

Dt1.Merge(Dt2); 

四、分页 

PagedDataSource pds = new PagedDataSource();
pds.AllowPaging = true;
pds.DataSource = dvIntegralExpense;
pds.AllowPaging = true;
pds.PageSize = pager.PageSize;
pds.CurrentPageIndex = pager.PageIndex;
rptIntegralExpense.DataSource = pds;
rptIntegralExpense.DataBind(); 

五、统计

DataTable实现统计,调用Compute函数,函数原型:

public object Compute(string strExpression,string strFilter) 

参数: 

strExpression:要计算的表达式字符串,基本上类型于Sql Server中的统计表达式

strFilter:统计的过滤字符串,只有满足这个过滤条件的记录才会被统计

调用举例:table.Compute("Count(*)","Sex=0")


资料整理,非原创

原文地址:https://www.cnblogs.com/sydeveloper/p/2988481.html