是否可以查看DataTable Row中的下一个值?

时间:2011-12-08 19:28:48

标签: vb.net

我有一个包含4行的DataTable。我想将第1行中一列中的值与第2行中的值进行比较。类似于:

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

3 个答案:

答案 0 :(得分:4)

您跟踪最后一项:

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

答案 1 :(得分:2)

您始终可以使用其索引(DataRowCollection.Item)访问DataRow

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

答案 2 :(得分:1)

说你有下表:

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