How to Change the Ordinal of DataColumn

How to change the ordinal or position of the DataColumn to the specified ordinal or position? Is it possible to reorder Columns? If yes, please share a sample script.

Issue/Query: How to change the ordinal or position of the DataColumn to the specified ordinal or position?

Resolution steps:

  • The above scenario is achievable with the help of Set Ordinal method.

Sample Excel/ DataTable Data: Consider the below data

  1. Launch UiPath Studio and create a sample process.
  2. Ensure to have UiPath.Excel.Activities installed in Manage packages.
  3. From the Activities pane, search for Excel application scope/ Read Range (Workbook)/ Read CSV.
  4. For Illustration, drag 'Excel Application Scope' and 'Read Range' activity to the Designer pane to read the excel file.
  5. Ensure to set the below parameters:
Excel Application scope:
- Workbook path
- AutoSave (Optional)
- CreateNewFile (Optional)
- Read Only (Optional)
- Visible (Optional)
Read Range:
- Range
- SheetName
- AddHeaders (Optional)
- DataTable

image.png

  • From the Activities pane, search for Invoke method activity and drag the same to the Designer pane.
  • Ensure to set the below parameters in property pane of Invoke method activity:

- TagretType: Null
- TargetObject – readRangeOutput.Columns("EmployeePayCheck(in $)")
- MethodName – SetOrdinal

- Expand the Parameter property and set the 'In' parameter to 0 (See below)
Note: 'In' parameter is set based on column Indexing.

image.png

image.png

  • The amended DataTable can now be further used for processing or for writing to Excel.
  • To view the amended DataTable, drag 'Output Data Table' activity to the designer pane and set the Input(Datatable) and output (String) parameters.
  • Use a Log Message activity/Message Box/Write Line to print the output to the console.

image.png

Read more on Forum post - How to Change Data Column .
1 Like