您的位置:首页 > 其它

筛选一个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;//返回的查询结果

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐