您需要Row
使用第一个值创建一个新的dr
.A DataRow
只能属于一个DataTable
.
您还可以使用Add
带有一组值的数据:
myTable.Rows.Add(dr.ItemArray)
或者甚至更好:
// This works because the row was added to the original table. myTable.ImportRow(dr); // The following won't work. No data will be added or exception thrown. var drFail = dt.NewRow() drFail["CustomerID"] = "[Your data here]"; // dt.Rows.Add(row); // Uncomment for import to succeed. myTable.ImportRow(drFail);
试试这个:
DataTable dt = (DataTable)Session["dtAllOrders"]; DataTable dtSpecificOrders = dt.Clone(); DataRow[] orderRows = dt.Select("CustomerID = 2"); foreach (DataRow dr in orderRows) { dtSpecificOrders.ImportRow(dr); }
yourTable.ImportRow(dataRow);
这是因为你要复制的行不一样TableName
:
例如,尝试:
Table1.TableName = "Table1"; Table2.TableName = "Table2";