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

来源:互联网 发布:唐小僧 理财优化服务 编辑:程序博客网 时间:2024/06/11 16:00

一、排序

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")


资料整理,非原创


<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击