筛选一个DataTable的数据,赋值给另外一个DataTable
2010-02-09 08:38
323 查看
有2个DataTable:DataTableA、DataTableB。
要求:
筛选DataTableA中,itemType字段值为book的数据,然后把筛选出来的数据,赋给DataTableB
实现:
DataView view = new DataView();
view.Table = DataTableA;
view.RowFilter = "itemType = 'book'";//itemType是DataTableA中的一个字段
DataTableB= view.ToTable();
或者:
DataRow[] rows = DataTableA.Select("itemType = 'book'");
DataTableB= DataTableA.Clone();
foreach (DataRow row in rows)
{
DataTableB.ImportRow(row);
}
或者
/// 执行DataTable中的查询返回新的DataTable
/// </summary>
/// <param name="dt">源数据DataTable</param>
/// <param name="condition">查询条件</param>
/// <returns></returns>
private DataTable GetNewDataTable(DataTable dt, string condition,string sortstr)
{
DataTable newdt = new DataTable();
newdt = dt.Clone();
DataRow[] dr = dt.Select(condition,sortstr);
for (int i = 0; i < dr.Length; i++)
{
newdt.ImportRow((DataRow)dr[i]);
}
return newdt;//返回的查询结果
}
要求:
筛选DataTableA中,itemType字段值为book的数据,然后把筛选出来的数据,赋给DataTableB
实现:
DataView view = new DataView();
view.Table = DataTableA;
view.RowFilter = "itemType = 'book'";//itemType是DataTableA中的一个字段
DataTableB= view.ToTable();
或者:
DataRow[] rows = DataTableA.Select("itemType = 'book'");
DataTableB= DataTableA.Clone();
foreach (DataRow row in rows)
{
DataTableB.ImportRow(row);
}
或者
/// 执行DataTable中的查询返回新的DataTable
/// </summary>
/// <param name="dt">源数据DataTable</param>
/// <param name="condition">查询条件</param>
/// <returns></returns>
private DataTable GetNewDataTable(DataTable dt, string condition,string sortstr)
{
DataTable newdt = new DataTable();
newdt = dt.Clone();
DataRow[] dr = dt.Select(condition,sortstr);
for (int i = 0; i < dr.Length; i++)
{
newdt.ImportRow((DataRow)dr[i]);
}
return newdt;//返回的查询结果
}
相关文章推荐
- 筛选一个DataTable的数据,赋值给另外一个DataTable
- 筛选一个DataTable的数据,赋值给另外一个DataTable
- DataTable筛选数据将筛选后的值赋值给其他DataSet
- 把一个表中的一列数据赋值给同一个表中的另外一列
- dataframe筛选数据根据某一个列的数据在另外的一个数组中
- [转]将dataTable里的一行赋值给另外一个DataTable
- 一个DataTable的数据如何导入另外一个DataTable中!!
- excel统计另外一个sheet的筛选数据
- 表示用于排序、筛选、搜索、编辑和导航的 DataTable 的可绑定数据的自定义视图
- 筛选DataTable数据的方法
- 查找某个数据,找到后把符合条件数据的一行复制到另外一个地方
- 从数据库查询的数据作为参数去查另外一个数据库
- 11-28 自己写一个数据集DataSet---ds,向其中添加一个数据表DataTable ---dt,并创建该表的列和行。然后将本本表作为数据源绑定到GradView中。
- 表更新(将一个表的数据更新到另外一个表)
- 如何将数据添加到自定义的DataTable中并赋值给list实现打印功能。
- update数据从一个表到另外一个表中
- C# 直接创建一个DataTable,并为之添加数据(自定义DataTable)
- 利用c语言将一个文件中满足某条件的数据 保存到另外一个文件中
- 如果在一个KeyFigure的Rule里面,使用另外一个KeyFigure的数据,如何实现?
- 一个APK中是否可以读写另外一个APK的SharedPreferences数据