Get even or odd rows from a DataTable using UiPath
While automating several business processes we will work with DataTables.
So, let’s see how we can get the even or odd rows from a datatable.
There are several ways to achieve any task in rpa. This is one of those ways to achieve this task.
You are at right place if this works for you.
Let’s jump into the integration part now:
Step1: Drag and Drop a Build DataTable Activity and create a DataTable as shown below:
Step2: Drag and Drop an Add Data Column Activity to add a new column which is ‘Status’.
Step3: As we added the new Status column, now we add the row value based on differentiating even and odd rows.
Follow as shown below to add the value to status column.
We will be adding ‘Yes’ for even rows and ‘No’ for odd rows.
In the if condition we are getting the row index from DataTable and checking even or odd row index value.
The new DataTable looks like below:
So, why i added the extra column at the end of the datatable?
Because now by just using a simple LINQ Query and get all the even or odd rows from a DataTable.
Step4: Drag and Drop an Assign Activity and create a new DataTable variable and provide the code in assign activity as shown below:
Here the status column which has Yes gives even rows of a DataTable.
Even row is 0,2,4…
Odd row is 1,3,5….
The output datatable for the even rows of datatable as shown below:
So, if you want to get the odd rows from a datatable then select the rows of a status column which has ‘No.
Then you will the odd rows from a DataTable.
Step4: If you want to write this DataTable again to excel then we need to remove the extra column that is added at the end.
Drag and Drop an Remove Data Column Activity as shown below:
Now this will look like the exact datatable that we used at the start.
By using a write range you can write it to a excel.