At a very basic level the following is what I am trying to achieve. I have a DataTable containing information read from a PSV file. Wherever a certain condition is met on a row, I need to insert an empty row in between. I am having trouble as it doesn’t seem like I can use Add Data Row inside a For Each Row loop.
A
B
C
A
B
C
A
B
C
Bot needs to output… IF: row.tostring.contains(“C”) then Add Data Row (empty line at CurrentRow+1)
A
B
C
A
B
C
A
B
C
How can I achieve the above to insert empty rows between rows inside a loop?
Basically, you cannot alter the datatable while you are iterating through it. What I suggest is to use a different output datatable and add rows as you iterate:
Thanks so much! This steered me in the right direction and the solution is working now.
More specifically, I am reading a text file that contains PSV rows and now it outputs correctly as such:
Column1|Column2|Column3
Text
Text
Field1|Field2|Field3
Text
Text
Field1|Field2|Field3
Text
Text
Field1|Field2|Field3
OUTPUTS TO:
Column1|Column2|Column3
Text
Text
Field1|Field2|Field3
Text
Text
Field1|Field2|Field3
Text
Text
Field1|Field2|Field3
For anyone in a similar situation, the above method worked where I stored the data in 2 different Data Tables, one for the input data, one for the output data. It basically adds every row from dt_input to dt_output, but if contains “|” then also adds empty row.
PLUS. On top of this, unrelated, I had to use .contains(“|”) but ignore the header row. To do this I had a counter int32 variable, and if counter>0 then adds empty row. if counter=0 it did nothing but counter=counter+1. This way I was able to add the empty rows for all rows containing “|” except for the header row as it ignores the first time