havin havin - 5 months ago 30
Vb.net Question

Combine 2 Datatable

I have 2 datatable

First datatable has only column name

Ex: A B

Second Datatable has column name with values

1 2 3 4
2 3 4 5

Now how can i get the values from second datatable to first datatable with columns matching

End result of First Datatable should be like

1 2

I tried merge but datatype mismatch occurs


You can use the DefaultView.ToTable method that allows you to specify which columns you want in the new Table

In this example I use the variable tableWithData to represent the datatable with the value you want to extract and the variable tableWithoutData to represent the table with only the column names. The variable newTable is another datatable that receives the result of the ToTable method, but you could also use tableWithoutData as the destination of the call.

Dim newTable = tableWithData.DefaultView.ToTable(False, tableWithoutData.Columns _
      .Cast(Of DataColumn)() _
      .Select(Function(x) x.ColumnName) _

The second parameter of the ToTable method is a ParamArray with the names of the columns you want to get from the source table. To get this value we could use a bit of Linq over the destination table to extract the column names