// Create a DataTable
DataTable table = new DataTable()
...
// Filter and Sort expressions
string expression = "[Birth Year] >= 1983";
string sortOrder = "[Birth Year] ASC";
// Create a DataView using the table as its source and the filter and sort expressions
DataView dv = new DataView(table, expression, sortOrder, DataViewRowState.CurrentRows);
// Convert the DataView to a DataTable
DataTable new_table = dv.ToTable("NewTableName");
7
DataTable dt = new DataTable();
DataRow[] dr = (DataTable)dsData.Tables[0].Select("Some Criteria");
dt.Rows.Add(dr);
2
Simple way is:
// dtData is DataTable that contain data
DataTable dt = dtData.Select("Condition=1").CopyToDataTable();
// or existing typed DataTable dt
dt.Merge(dtData.Select("Condition=1").CopyToDataTable());
3
DataTable dt = new DataTable();
foreach (DataRow dr in drResults)
{
dt.ImportRow(dr);
}
DataTable dt = new DataTable();
dt = dsData.Tables[0].Clone();
DataRows[] drResults = dsData.Tables[0].Select("ColName = 'criteria');
foreach(DataRow dr in drResults)
{
object[] row = dr.ItemArray;
dt.Rows.Add(row);
}
1
DataTable Assetdaterow =
(
from s in dtResourceTable.AsEnumerable()
where s.Field<DateTime>("Date") == Convert.ToDateTime(AssetDate)
select s
).CopyToDataTable();
80
任何人都需要它在VB.NET中:
Dim dataRow as DataRow
Dim yourNewDataTable as new datatable
For Each dataRow In yourArray
yourNewDataTable.ImportRow(dataRow)
Next
13 回答
为什么不迭代你的DataRow数组并添加(如果需要,使用DataRow.ImportRow来获取DataRow的副本),例如:
确保您的dataTable与DataRow阵列中的DataRows具有相同的架构 .
For .Net Framework 3.5+
但是如果数组中没有行,则可能导致错误,例如source不包含DataRows . 因此,如果您决定使用此方法
CopyToDataTable()
,则应检查数组以确定它是否具有数据行 .MSDN上的参考资料:DataTableExtensions.CopyToDataTable Method (IEnumerable)
另一种方法是使用DataView
Simple way is:
这是解决方案 . 它应该工作正常 .
任何人都需要它在VB.NET中:
.Net 3.5添加了DataTableExtensions,使用DataTableExtensions.CopyToDataTable方法
对于datarow数组,只需使用.CopyToDataTable(),它将返回数据表 .
对于单数据流使用
您可以像这样使用System.Linq: