Hey Community, I have two dataTables: DT1 and DT2, these two dataTables have a one commun column “ID” and I want to copy values from column named “Col1” in DT1 to another column named “Col2” in DT2 based on column “ID”
Hi @BlueBird1
You can use the following query:
For Each row In DT1.Rows
Dim idValue As String = row("ID").ToString
Dim matchedRow As DataRow = DT2.AsEnumerable().Where(Function(x) x("ID").ToString = idValue).FirstOrDefault()
If matchedRow IsNot Nothing Then
matchedRow("Col2") = row("Col1").ToString
End If
Next
Hope this helps,
Best Regards.
I tried your code and I got this error
Read Range: A column named ‘col2’ already belongs to this DataTable.
I know that my data table contain this column, and I dont want to add the column I just want to store the values of col2 in col1 based on “ID”
@BlueBird1
hello again.
try using dictionary if data is less.
read first table dt1
create one dictionary (Dict)(of string, string) or (of string, Object) add key as first table’s ID column and and value as “Col1” as you need this in second table asTb2
while itterating Provide for each row while providing dt1 instead provide dt1.DefaultView.ToTable(true,“ID”,“Col1”) it will gie unique values for dict in assign activity provide Dict(CurrentRow(“ID”).tostring)=CurrentRow(“Col1”).tostring
read second table add one column name “Col2” or if its there no worry. itterate it with for each row give assign currentrowTb2(“Col2”)= Dict(CurrentRowTb2(“ID”).tostring