How to add Column header in existing DataTable

In UiPath, if you have a DataTable without column headers and want to add column headers, you can do this in a few ways depending on your situation.

Case 1: You want to set custom column headers (e.g:- “Name”, “Age”, “City”)

Steps:-

  1. Use “Assign” activity to create a new DataTable with proper column headers.

  2. Use a “For Each Row” loop to import rows from the old DataTable into the new one.

Example:

// Step 1: Create new DataTable with headers
newDT = New DataTable
newDT.Columns.Add(“Name”)
newDT.Columns.Add(“Age”)
newDT.Columns.Add(“City”)

// Step 2: Import data from oldDT
For Each row In oldDT.Rows
newDT.Rows.Add(row.ItemArray)

Case 2: Your data’s first row contains the headers

  1. Use “Build Data Table” or manipulate with logic like this:
  2. Read the first row from your original DataTable.
  3. Create a new DataTable, using the first row’s values as column names.
  4. Add the rest of the rows from the original DataTable to the new one.

Example:

// Step 1: Get header row
headerRow = oldDT.Rows(0)

// Step 2: Create new DataTable with headers from first row
newDT = New DataTable
For Each item In headerRow.ItemArray
newDT.Columns.Add(item.ToString)
Next

// Step 3: Add the rest of the data (excluding header row)
For i = 1 To oldDT.Rows.Count - 1
newDT.Rows.Add(oldDT.Rows(i).ItemArray)
Next

Happy automation