Add row to ARRAY of DataRows - DataRow[]

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 Likes

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

2 Likes

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

Regards
Balamurugan

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:

1 Like

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

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

Regards
Balamurugan

1 Like

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.

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

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.

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

I have tested it.

Regards
Balamurugan

1 Like

Hi @FlpVsg,

in the final you can convert like this

image

Regards
Balamurugan

2 Likes

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.

1 Like

It worked quite well!
Thanks a lot.

Regards

: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:

1 Like

That would work too.
Thanks for your help!

:smiley:

1 Like