DataGridViewからDataTableを取得


DataGridViewでソートをしたときなどにバインドしているDataTableとRowIndexがずれてしまう。
以下、DataGridViewのRowIndexよりDataTableのRowIndexを取得する例。

'DataGridViewのRowオブジェクトを取得
Dim Dtrow As System.Data.DataRowView = CType(Me.dataGridView.Rows(i).DataBoundItem, System.Data.DataRowView)

'DataGridViewのRowオブジェクトからDataRowを取得
Dim row As System.Data.DataRow = CType(Dtrow.Row, System.Data.DataRow)

'DataRowのインデックスを取得
Dim dtIndex As Integer = row.Table.Rows.IndexOf(row)


動作確認環境:Visual Stadio 2008