Lost records in DataView

What should you do if you need to get position of a record in DataView? Generally, DataView implements an IList interface, so you may concider calling ((IList) dataView).IndexOf(dataRowView) would do all the magic for you.
I thought in such a way before I found out, that calling IndexOf on a DataRowView being edited would return “-1″ (i.e. not found).

It happens because a DataRow creates a temporary copy of itself when calling BeginEdit() and associates a DataRowView with that copy. And calling IndexOf on DataView performs lookup through DataTable’s index, who has no idea about that temporary copy and returns nothing.

So you should finish with editing before performing any lookup.

Leave a Reply