Update Next row of the data Table

Hi,

I want to update next row of the datable how can we achive this .

Example
I am looking for word “Forum” in the data table…
When I found the “Forum” value I want to updates it’s next row

Col1 >> output
Forum >>
Test >> Forum
Forum >>
ABC >> Forum
Forum >>
Xyz. >> Forum

Like above where I found forum in Col1 I want to update next output column

*Note >> is just column seperator

can you share the datatable once in excel format

You can do it classical with a for each row activity and if check

For each row | define idx for the output (so we can refer to the looped row index)

  • IF: row(Col1).toString.Equals(“YourCheckTerm”)
    • Then: YourDataTableVar.Rows(idx + 1)(YourColNameOrIndex) = newValue

image

Thanks @ppr
can this can be achievable without foreach loop ?

As an alternate you can retrieve all row indexes for Col1 forums with a Linq

arrIndexes = Enumerable.Range(0,YourDTVar.Rows.Count).Where(Function (x) YourDTVar.Rows(x)("Col1").toString.Equals("YourSearchTerm")).toArray

then iterate over the indexes and with YourDTVar.Rows(idx + 1)(YourColNameOrIndex) = NewValue you can update the value

For Both mentioned approaches maybe you will extend a last row check (as there is not a LastRow + 1 index). We also can modify / doing some variations on the mentioned approaches as well

1 Like

Thanks @ppr for your response

I will try this way

When playing also have a look on this variation:

arrMyNextIndexes =

(From i in Enumerable.Range(0,YourDTVar.Rows.Count)
Where YourDTVar.Rows(i)("Col1").toString.Trim().Equals("YourSearchTerm")
Where Not i + 1 >= YourDTVar.Rows.Count
Select x = i + 1).toArray

With this variation we have already calculated the next row index and handled the last row check

Updating the values:

For each acitvity | item in arrMyNextIndexes

  • Assign: YourDTVar.Rows(i)(ColNameOrIndex) = newValue

For 1 column value update we would not recommend LINQ /RowArray reconstruction approaches. Also with the calculated indexes we had minimized the loop efforts.

For more info about the topic also have a look here:
How to Update Data Column Values of a Data Table | Community Blog