Copy a Datacolumn from a table to another  
Author Message
spOAMethodusage





PostPosted: Mon Oct 01 07:29:06 PDT 2007 Top

Visual C#.Net >> Copy a Datacolumn from a table to another

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?

DotNet19  
 
 
Ignacio





PostPosted: Mon Oct 01 07:29:06 PDT 2007 Top

Visual C#.Net >> Copy a Datacolumn from a table to another Hi,



> 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


 
 
Manuel





PostPosted: Mon Oct 01 09:32:35 PDT 2007 Top

Visual C#.Net >> Copy a Datacolumn from a table to another Ignacio Machin ( .NET/ C# MVP ) ha scritto:
> Hi,
>


>> 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
 
 
SomNathShukla





PostPosted: Tue Oct 02 20:11:00 PDT 2007 Top

Visual C#.Net >> Copy a Datacolumn from a table to another 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.



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


> >> 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
>