Next/previous row item in data table

Hi, I’m working on a project where I have to use data table and compare a particular column value of current row item with the next/previous row’s column value. Unable to do that, Kindly help me accomplish.


Hey @prabirs

You can follow below steps:

  1. First find the value from datatable that particular column value by using "Datarow[ ] results = Datatable.Select("[ColB]=‘your value’")
  2. The above step will return “Datarow[ ]
  3. Iterate with Datarow[ ] by using For-each activity with Type Argument=System.Data.Datarow.
  4. Well assuming you have distinct records with values then just find the row index
    "Int value_row_index =yourdatatable.Rows.Indexof(results(0))
  5. Then you can get Next Row’s Column value like this
    String next_col_value = yourdatatable.Rows((value_row_index+1))(“your_col_name”)
  6. Similarly you can find previous row’s Column value like this
    String prev_col_value = yourdatatable.Rows((value_row_index-1))(“your_col_name”)
  7. now perform your comparison steps with above results.



Thanks aksh for ur help. It’s working.

Hello Akshay,

If I use above method I am getting error as object cannot be converted to string, below is screen shot please help.

Hi @harinathreddy.yn,

Try converting it to string, as the error shows it is object and you are trying to assign it to a string variable.

Ex: dt.Rows((rowIndex))(“Invoice No.”).ToString

It worked Sasi, some times we (especially I) miss basics thanks.