我需要将数据视图复制到数据表中。似乎这样做的唯一方法是逐项遍历数据视图并复制到数据表中。一定有更好的方法。
问问题
54706 次
2 回答
57
dt = DataView.ToTable()
或者
dt = DataView.Table.Copy()
,
或者
dt = DataView.Table.Clone()
;
于 2009-04-20T19:31:36.343 回答
3
答案不适用于我的情况,因为我有带有表达式的列。DataView.ToTable()
只会复制值,而不是表达式。
首先我尝试了这个:
//clone the source table
DataTable filtered = dt.Clone();
//fill the clone with the filtered rows
foreach (DataRowView drv in dt.DefaultView)
{
filtered.Rows.Add(drv.Row.ItemArray);
}
dt = filtered;
但该解决方案非常慢,即使只有 1000 行。
对我有用的解决方案是:
//create a DataTable from the filtered DataView
DataTable filtered = dt.DefaultView.ToTable();
//loop through the columns of the source table and copy the expression to the new table
foreach (DataColumn dc in dt.Columns)
{
if (dc.Expression != "")
{
filtered.Columns[dc.ColumnName].Expression = dc.Expression;
}
}
dt = filtered;
于 2013-06-26T16:49:55.447 回答