How To Reorder Columns In DataTable – In UiPath

Sometimes we might be asked to change the order of the columns in the excel and then process the data.

SetOrdinal method can be used to achieve the above.

Let’s see how to use it in the UiPath!!!

UiPath Reorder Datatable Columns 1.0.png
UiPath Reorder Datatable Columns 2

Note:
1.ColumnName – The name of the column for which we want to change the position.
2.Parameter –
 To which position or index, we want to move the column to(Indexing starts from zero).


Example

Implementation using UiPath :

Let us implement a workflow which reads an excel file and then we will try to reorder the columns.

Step 1:
Drag “Excel Application Scope” and “Read Range” activities into the design panel to read the excel file and pass the required parameters.

UiPath Reorder Datatable Columns 3.PNG

Step 2:
Drag “Invoke Method” activity into the design panel and populate it with the below-mentioned code.

Note:
TargetObject – dt_Sample.Columns(“Country”)
MethodName – SetOrdinal
In Parameter – 0(0 indicates first position ,for our process)

UiPath Reorder Datatable Columns 1UiPath Reorder Datatable Columns 2

Step 3:
Drag “Output Data Table” activity into the design panel and convert Datatable to String type.

UiPath Reorder Datatable Columns 4UiPath Reorder Datatable Columns 5

Step 4:
Drag “Message Box” activity into the design panel to display the modified data table.

UiPath Reorder Datatable Columns 6

Step 5:
Finally, let’s run the project.
We can see the modified DataTable with reordered column names!!!

UiPath Reorder Datatable Columns 7.PNG

Click here to download the Source Code…

Hope it has helped you…

Leave a Reply

%d bloggers like this: