When working with DataTables in UiPath, there may be times when you need to rearrange the order of columns. One efficient way to achieve this is by using the Invoke Method activity. This method allows you to use .NET methods to manipulate DataTables directly. In this blog post, we’ll explore how to change column positions using the Invoke Method activity in UiPath.
Step 1: Setting Up Your Environment
Before we begin, ensure you have UiPath Studio installed and set up. Open a new project and add the necessary dependencies, such as UiPath.Excel.Activities if you’re working with Excel files.
Step 2: Reading the Data Table
First, we need to read the data table. If your data is in an Excel file, use the Read Range activity to load it into a DataTable variable.
- Drag and drop the Excel Application Scope activity.
2. Provide the path to your Excel
3. Inside the scope, use the Read Range activity.
4. Set the output to a DataTable variable, e.g., ` DtCustDetails `.
Step 3: Defining the New Column Order
Next, define the new order of columns as a list of strings. This list will be used to reorder the columns in your DataTable.
1. Create a variable of type `List<String>` to hold the new column order.
2. Initialize the list with the names of the columns in the desired order.
For example:
Step 4: Creating the Invoke Method Activity
Now, we’ll use the Invoke Method activity to rearrange the columns.
- Drag and drop the Invoke Method activity.
2. In the TargetObject field, select the DataTable variable, `dtInput`.
3. Set the MethodName to `SetOrdinal`.
Step 5: Reordering the Columns
For each column in the new order, use the Invoke Method activity to change its position.
1. Use a For Each activity to iterate through the list of column names in the new order.
2. Inside the loop, add an Invoke Method activity.
3. Set the TargetObject to the specific column you want to move, using the following expression:
dtInput.Columns(columnName)
4. Set the MethodName to SetOrdinal.
5. In the **Parameters** section, add an argument of type `Int32` for the new column index.
Here’s a sample workflow inside the **For Each** activity:
– For Each: Iterate through `newColumnOrder` (with index `currentIndex`).
– Invoke Method:
– TargetObject: `dtInput.Columns(columnName)`
– MethodName: `SetOrdinal`
– Parameters: Add a new parameter with the value `currentIndex`.
Step 6: Writing the Data Back to Excel
If you want to save the rearranged DataTable back to an Excel file:
1. Use the Write Range activity.
2. Provide the path to the new or existing Excel file.
3. Specify the DataTable variable, `dtInput`, as the input.
### Complete Example Workflow
Here’s a simplified version of what your workflow might look like:
1. Read Range: Read the original data into `dtInput`.
2. Assign: Initialize `newColumnOrder` with the desired column order.
3. For Each**: Iterate through `newColumnOrder`.
– Invoke Method: Set column ordinal for each column in `newColumnOrder`.
4. Write Range** (Optional): Write `dtInput` to an Excel file.
Conclusion Using the Invoke Method activity to change column positions in a DataTable is a powerful approach in UiPath. By leveraging .NET methods, you can directly manipulate DataTables and customize their structure to meet your needs. This method is particularly useful when dealing with complex data transformations and ensuring your data is organized correctly for further processing.