Copying columns between Data Tables

Hi Friends,
I need your help. How can we copy a particular column from one data table to another data table?

For Example:
Let’s assume that we have 2 data tables with names DT1 and DT2. I have to copy a column with the name say “Location” from DT1 to the column by the name “Area” in DT2. Can anyone help me with the same.

regards,
Ananthu

Hi @Ananthu,

You can use for each row activity to update the value in the another datatable.

In DT2 you have only one column-Area?

Regards,
Arivu

2 Likes

Hi @arivu96,
Nope ! That’s the one leading to this confusion. I have multiple columns in DT2 as well. So, is there any option to copy one specific column from DT1 to another specific column in Dt2.

Thanks & Regards,
Ananthu

HI @Ananthu,

you can get the particular column alone to another data table using below code.

Dt2=Dt1.DefaultView.ToTable(False,"Location")

so you can move the particular column alone in another data table.

if suppose Dt2 already having some data you need add newly use for each data row to update the area column values.

Regards,
Arivu

2 Likes

Hi @arivu96,
So, you’re asking me to copy that particular column in DT 1 to a new data table. Later, copy that new data table to that particular column in DT2 right?

I thought of the same thing. I will try that and will let you know.

Is there any other simple solution ? :wink:

Thanks & Regards,
Ananthu

how to copy particular column of data to another datatable of particular column name?

Using Linq query?

Did Anyone find out a way to do this?

I have a similar scenario where you need to copy a particular column values from a data table to another column name in another data table.

1 Like

dt_New=dt_Original.DefaultView.toTable(false,”Column1″,”Column2″,…)
cheers @syedbahauddin

2 Likes

Hi @syedbahauddin,

Is there any specific condition to add the data from the another table?

Regards,
Arivu

Thank you Pradeep.

Here is the detailed scenario.

I would like to copy and paste values from one column of a data table to another column of another data table. Please have a look at the below Screenshot.

I would like to copy all the columns values from first table and paste it to the new Datatable (shown below).

For example : Name to Full name , Address to Area and Country to Country/City

I have taken column by column approach. please advise if its the best practice.

Note: The second data table is empty 1

Please look at the scenario in my comment

Hi @syedbahauddin,

Use for each row activity and append the value into another table.

dt1(Name,Address,Country)

dt2(Full Name, Gender,Area,Salary,Country)

For each row (dt1)
add data row activity
Properties
DataTable : dt2
DataRow : {row("Name").ToString(),"",row("Address").ToString(),"",row("Country").ToString()}

Regards,
Arivu

3 Likes

Hi Arivu,

Thank you so much. Works perfectly fine.

Best Regards,
Baha