Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to Copy datacolumn from one data table to another

Tags:

c#

.net

datatable

How can I copy 1 data column from 1 data table to a new datatable. When I try to do it, I get the error Column 'XXX' already belongs to another DataTable.?

dataColumn = datatable1.Columns[1];
datatable2 = new DataTable();
datatable2.Columns.Add(dataColumn);

Thanks in Advance

like image 304
Ananth Avatar asked Nov 15 '10 13:11

Ananth


People also ask

How do I copy from one DataTable to another?

The simplest way is to clone an existing DataTable, loop through all rows of source DataTable and copy data from column by column and add row to the destination DataTable.


3 Answers

You cannot copy DataColumns. What you'll need to do is create a new DataColumn in the new datatable with the same data type as in the old datatable's column, and then you need to run a FOR loop to bring in all the data from the old datatable to the new datatable.

See the following code. This assumes that the datatables have exactly the same number of rows.

DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();

dt2.Columns.Add("ColumnA", dt1.Columns["ColumnA"].DataType);

for (int i = 0; i < dt1.Rows.Count; i++)
{
    dt2.Rows[i]["ColumnA"] = dt1.Rows[i]["ColumnA"];
}

Also, If the data you are copying are reference types and not value types you might want to see if a .Clone() method is available for the type, or make one yourself. Just doing 'this = that' in the FOR loop will not work on reference types.

like image 187
Lane Avatar answered Oct 22 '22 04:10

Lane


You cannot copy a DataColumn. (DataColumns are very tightly coupled with their tables)

Instead, you can add a new column with the same name and datatype.

You might be looking for DataTable.Clone(), which will create a structual copy of an entire table. (With the same schema, but no data)

like image 29
SLaks Avatar answered Oct 22 '22 04:10

SLaks


Just a thought, are your DataTables both in the same DataSet?

If so, you can create a named DataRelation between the columns of two tables (think foreign key).

Then you can add a Calculated DataColumn to your table that has its Expression property set to "Child(RelationName).ColumnName" or "Parent(RelationName).ColumnName" depending on the direction of the relationship.

This will give you the same effect as copying the column, but I believe it only evaluates it lazily. So maybe it will give you what you need.

There is an example here of how this works. The example uses the Sum aggregate function, but you just need to reference the column name and it will duplicate it in your DataTable

myDataSet.Relations.Add(
    "Orders2OrderLines", 
    myDataSet.Tables["Orders"].Columns["OrderID"], 
    myDataSet.Tables["OrderLines"].Columns["OrderID"]);

ordersTable.Columns.Add("OrderTotal", typeof(decimal), "Sum(Child(Orders2OrderLines).ExtendedPrice)");

HTH

like image 22
sheikhjabootie Avatar answered Oct 22 '22 03:10

sheikhjabootie