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:-
-
Use “Assign” activity to create a new DataTable with proper column headers.
-
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
- Use “Build Data Table” or manipulate with logic like this:
- Read the first row from your original DataTable.
- Create a new DataTable, using the first row’s values as column names.
- 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