Copy a Datacolumn from a table to another

  • Thread starter Thread starter Manuel
  • Start date Start date
M

Manuel

Hi to all,
I'm trying to copy a Datacolumn from a table to another, but with this code:

destTable.Columns.Add(srcTable.Column["colname"]);

I got this error:

Column 'colname' already belongs to another DataTable.

Anyone can help me?
 
Hi,

Manuel said:
Hi to all,
I'm trying to copy a Datacolumn from a table to another, but with this
code:

destTable.Columns.Add(srcTable.Column["colname"]);

I got this error:

Column 'colname' already belongs to another DataTable.

You have to recreate the column and copy the data yourself, unfortunatelly
DataColumn does not implement a Clone method
 
Ignacio Machin ( .NET/ C# MVP ) ha scritto:
Hi,

Manuel said:
Hi to all,
I'm trying to copy a Datacolumn from a table to another, but with this
code:

destTable.Columns.Add(srcTable.Column["colname"]);

I got this error:

Column 'colname' already belongs to another DataTable.

You have to recreate the column and copy the data yourself, unfortunatelly
DataColumn does not implement a Clone method
Thanks for the reply
 
u can use this
DataTable dt1 = new DataTable();
dt1.Columns.Add("hi");
DataTable dt2 = new DataTable();

DataColumn dc = new DataColumn();
dc.ColumnName = dt1.Columns["hi"].ColumnName;
dc.DataType = dt1.Columns["hi"].DataType;

dt2.Columns.Add(dc);

here i am creating new column and seting properties of new column from
previous cloumn properties . u can set as more propery as u want. nowur new
column will same as prevoius one . now add this to new table.
u can not add single column to two different table.

Manuel said:
Ignacio Machin ( .NET/ C# MVP ) ha scritto:
Hi,

Manuel said:
Hi to all,
I'm trying to copy a Datacolumn from a table to another, but with this
code:

destTable.Columns.Add(srcTable.Column["colname"]);

I got this error:

Column 'colname' already belongs to another DataTable.

You have to recreate the column and copy the data yourself, unfortunatelly
DataColumn does not implement a Clone method
Thanks for the reply
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top