从DataTable过滤和获取某行记录

问题“我有一个DataTable,怎样获取某笔记录“?

DataTable如下:

 1  private DataTable UserInfor()
 2     {
 3         DataTable Dtable = new DataTable();
 4         Dtable.Columns.Add("ID", typeof(int));
 5         Dtable.Columns.Add("Name", typeof(string));
 6         Dtable.Columns.Add("Address", typeof(string));
 7 
 8         Dtable.Rows.Add(1, "Leo Yang", "DongGuan");
 9         Dtable.Rows.Add(2, "Peter Zhong", "GuanZhou");
10         Dtable.Rows.Add(3, "Eric Peng", "ShenZhen");
11         return Dtable;
12     }


下面分种方式来演示。
第一个是使用DataView的RowFilter()方法:



绑定至Data控件:

this.GridView1.DataSource = dv;
this.GridView1.DataBind();


第二个方法:


第三个方法:


第四个方法,其实它是第三种方法的简化版:


第二,三和第四个方法,绑定至Data控件:

this.GridView1.DataSource = query.AsDataView().ToTable();
this.GridView1.DataBind();


第五个方法,使用DataTable的Select方法,可以实现查询多个字段。


绑定至Data控件:

this.GridView1.DataSource = query.CopyToDataTable();
this.GridView1.DataBind();


第六个方法:
http://www.cnblogs.com/insus/archive/2012/08/22/2650137.html

原文地址:https://www.cnblogs.com/insus/p/3075994.html