简体   繁体   中英

Is it possible to peek at the next value in a DataTable Row?

I have a DataTable that contains 4 rows. I want to compare a value in one column in row 1 with the value in row 2. Something similar to this:

For Each row As DataRow in drRows
  If row("column") <> row("column") 'I want the second row("column") to be the next row.
     'do something else
  End If
Next

You keep track of the last item:

Dim last As DataRow = Nothing

For Each row As DataRow In drRows
    If last IsNot Nothing Then
        ' Compare last with row
    End If

    last = row
Next

You can always access a DataRow with its index ( DataRowCollection.Item ):

For i As Integer = 0 To tbl.Rows.Count - 1
    Dim row As DataRow = tbl.Rows(i)
    If i <> tbl.Rows.Count - 1 Then
        Dim nextRow As DataRow = tbl(i + 1)
        If row("column").Equals(nextRow("column")) Then
            'do something"
        End If
    End If
Next

Say you have the following table:

A   B    C   D
1   2    3   4
5   6    7   8 
9   10   11  12
12  12   13  14
For i As Integer = 0 To dt.Rows.Count - 2
   If dt.Rows(i)("ColName") <> dt.Rows(i + 1)("ColName") Then    
       'Do something
   End If
Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM