Add row to ARRAY of DataRows - DataRow[]


#1

Hello,

I am trying to read new row into array of datarows.

  • First I loop through a datable using for each row, where “row” is my index.
  • Inside the loop I check some conditions and if they are matched I need to add “row” to my array of DataRows[].

OBS: I tried using “Datatable.Select” to fill the array but I didn’t work, so I am trying another approach.

Thanks.


#2

Hi @FlpVsg,
You have 2 possibilities.

  1. After condition matched use Add to collection Activity like below

image

  1. Create a temp DataTable like dt1.
    Before the loop take the structure of the source table to temp table
    dt1=SourceDatatable.Clone
    After condition matched Use Add data row activity

image

Regards
Balamurugan


#3

Hi @FlpVsg,
Check this sample for you
File :AddrowARRAY.zip (9.4 KB)

Regards
Balamurugan


#4

Thanks @balupad14.
But I feel as if I’m missing something out.

To be precise, my goal is to delete some rows from an Excel sheet.
At first, I read the original sheet into datatable d1.
Now, I need to apply criteria to delete some rows from dt1.
I tried using .Select, however the column I need to search has numbers and texts making my “like” invalid.

So I thought about this second approach so I could apply the criteria first, then get the whole datarow. It doesn’t seem very pratical having 3 datables in order to delete some rows from Excel Sheet :confused:


#5

Hi @FlpVsg,
install this package BalaReva.DataTable.Activities

you can use this activity Remove “Data Row Select”
Here is the example.

Regards
Balamurugan


#6

I think you need to simply use Add Data Row to the data table, then re-filter to your array of rows again and it will have that data row included. Now, saying that, I’m not sure if you are doing this inside a ForEach that references the rows, then you might need to restart the loop because the data table has changed at that point.

If you want to keep your loop, then you will need to duplicate your data table so you can change the data.

There are other ways too but you would need to calculate the row number in Excel (maybe dt1.Rows.Count+2, or whatever), and use Write Cell or Write Range with those coordinates. This solution would also require you to convert the column index to a char (like Char(65+index) )

So those are my thoughts.

Regards.


#7

Thanks for sharing @balupad14.
I am sure this will be very useful at some point. But unfortunately I can’t use the “Remove data row” feature as my select won’t work. I think the best scenario would be making it work (but I am still not sure if it’s even possible).

Trying your first solution (add to collection) I get an exception due to the collection not being initialized:
image

And here’s my collection:

image

LinhasParaDeletar (RowsToDelete) being DataRow[]
And row being my “index” in for each row for dt1.

As I was saying before, my goal is to delete all rows that start with anything different than “3” or “5”.
This is a sample of my column:

image


#8

Hi @ClaytonM,

I’m not sure if I got your point.
I am struggling to get certain rows into array of datarows :confused:
And yes I am using a for each row and I would manipulate that same row.

Thanks for sharing your thoughts anyway.


#9

Hi @FlpVsg,
Instead of array can you go to List
List below

I have tested it.

Regards
Balamurugan


#10

Hi @FlpVsg,

in the final you can convert like this

image

Regards
Balamurugan


#11

Maybe I misunderstood.
Are you wanting to
A) Delete row from Data Table
then B) Add deleted row to Array[]

Anyway, to create an array of Data Rows, you can use .Where (which is how I do it)

dt1.AsEnumerable.Where(Function(r) CInt(r(column).ToString.Trim) = 3 or CInt(r(column).ToString.Trim) = 5).ToArray

You don’t even need a For each loop if you use the above vb.net code
That just is used to create an Array based on a certain criteria.

It might be helpful if you provided a little screenshot of what you are trying to do in UiPath.

@balupad14 is probably more helpful than I am, lol

Regards.


#12

It worked quite well!
Thanks a lot.

Regards


#13

:smile::smile::smile::smile::smile::smile::stuck_out_tongue_winking_eye::stuck_out_tongue_winking_eye::stuck_out_tongue_winking_eye::stuck_out_tongue_winking_eye:


#14

That would work too.
Thanks for your help!

:smiley: