andy brandt's blog

finding the index of a datarow in the currencymanager

after much searching and reading many blogs and forums, i never found a comprehensive answer on the best way to get the index of a datarow from the currencymanager in vb.net. i found a few posts that led me in the right direction, not none that did exactly what i wanted. so this is what i came up with, i’m not sure if it’s the most efficient, but it seems to work.

' RowToFind is the DataRow we are looking for the index of
Dim cm As CurrencyManager = Me.BindingContext.Item(dataSource, dataMember)
Dim dv As DataView = DirectCast(cm.List, DataView)
Dim idx as Integer = -1

For i As Integer = 0 To dv.Count - 1
    If dv.Item(i).Row Is RowToFind Then
        idx = i
        Exit For
    End If
Next

cm.Position = idx

our problem with the previous method, was that when you deleted a row, it would return the wrong index. this appears to bypass that problem.