I want to move the data from a dataColumn to a specific column in my dataTable. I am not sure how to specify what column within my Datatable I want to add the datacolumn.
foreach (DataColumn col in dt.Columns)
{
dt1.Columns.Add(col);
}
I receive an exception Column 'X' already belongs to another DataTable.
You need to copy the properties like
ColumnNameand create newDataColumns:There’s a reason for the
ArgumentExceptionwhen you add aDataColumnwhich already belongs to another DataTable. It would be very dangerous to allow that since aDataTableholds a reference to their columns and every column holds a reference to it’s DataTable. If you would add a column to another table your code would blow sooner or later.If you also want to copy the
DataRowsinto the new table: