Set DataTable header by merging the value of 2 rows

Hello,

I have a DataTable like this :

image

I need to merge the values of the first 2 rows of each column and set the result value as header.

Do you have any suggestions for this issue?

Thanks in advance.

Hi,

Can you try the following sample?

Sample20231205-1L.zip (8.8 KB)

Regards,

Hello @SONYC

  1. Assign activity:
    dtResult = New DataTable

  2. For Each activity (ForEach col in dt.Columns):
    a. Assign activity:
    dtResult.Columns.Add(col.ColumnName, GetType(String))
    dtResult.Rows.Add(dt.Rows(0)(col.ColumnName).ToString & " " & dt.Rows(1)(col.ColumnName).ToString)

  3. Remove Data Rows activity (for removing the first two rows from the original DataTable):
    DataTable: dt
    Index: 1
    Number of Rows: 2

  4. Invoke Method activity:
    TargetObject: dt
    MethodName: Merge
    Parameters: dtResult

Thanks & Cheers!!!

1 Like

Thank you @Yoichi
It works exactly as expected.
Just for information, what would be the lambda query for this procedure in one shot ?
With LINQ the constraint would be to use a second DT I guess.

Hi,

It’s necessary 2 expressions. Can you try the following sample?

image

Dim d As DataTable = dt
dt.Columns.Cast(Of DataColumn).ToList().ForEach(Sub(c)
    c.ColumnName = d.Rows(0).Item(c).ToString()+" "+d.Rows(1).Item(c).ToString()
End Sub
)
dt = dt.AsEnumerable.Skip(2).CopyToDataTable()

Sample
Sample20231205-1Lv2.zip (8.7 KB)

Regards,

1 Like

Again, thank you @Yoichi !

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.