Haru Rosyadi Haru Rosyadi - 2 months ago 8
Vb.net Question

Find and get row index in datatable

DataTable.Select problem



i have simple datatable like this

| 1 | 2 | 3 |
|------|------|------|
| 1966 | 6544 | 1967 |
| 9560 | 3339 | 4968 |
| 0 | 9400 | 1765 |
| 0 | 5479 | 6701 |


for example i want to check if 1966 is already exist in column "1" and if it exist get row index
i do code like this

Dim search() As DataRow = table.Select(" '" & i & "' = '" & value & "' ")
'where i is a integer from 1 to 3 and value is a biginteger
If search.Count > 0 Then
'get row index
Else
Console.WriteLine("not found")
End If

Answer

Unless the row index is contained in the values of the rows themselves you'll have to abandon the Select method and use a cursor based approach. This example will stop looping if a match is found:

Dim intTargetIndex As Integer = -1
Dim intCursor As Integer = 0

Do Until intCursor = table.Rows.Count OrElse intTargetIndex > -1

    If table.Rows(intCursor)(0).ToString() = value.ToString() Then

        intTargetIndex = intCursor

    End If

    intCursor += 1

Loop
Comments