1. 方式一 导入row,新表架构与原表相同,foreach前clone, newDataTable=oldDataTable.Clone();
foreach (DataRow row in rowArray) { dataTable.ImportRow(row); }
2. 方式二 CopyToDataTable
DataTable dt = new DataTable(); DataRow[] dr = dt.Select("Your string"); DataTable dt1 = dr.CopyToDataTable(); if (dr.Length > 0) DataTable dt1 = dr.CopyToDataTable(); //https://msdn.microsoft.com/en-us/library/bb396189.aspx
3. Add
DataTable dt = new DataTable(); DataRow[] dr = (DataTable)dsData.Tables[0].Select("Some Criteria"); dt.Rows.Add(dr);
4. DataView
// 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");
5. Merge
// 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());
6.
DataTable dt = myDataRowCollection.CopyToDataTable<DataRow>();
7. Linq
if (dataRows != null && dataRows.Length > 0) { dataTable = dataRows.AsEnumerable().CopyToDataTable(); }