得到当前dgv的CurrentRow
转载
当希望在DataGridView行变化做一些动作,比如当前行的图片,控制等等... 不要使用bindingsource 的类似 "PositionChanged"、"CurrentChanged"、"CurrentItemChanged"等事件,当用这种事件获得DataGridView中的值还是上一条的值(后证实是使用方法不对,呵呵...最后将会列出方法) 而要改用DataGridView 的 CurrentCellChanged 事件,只是这个事件有一个弊端是当列改变是也是有效的(小控制一下就行,如下面用currentSourcePosition去控制) 如下
code dim currentSourcePosition = 0
Private Sub POSSALESLINEDataGridView_CurrentCellChanged( _
ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles POSSALESLINEDataGridView.CurrentCellChanged
If _formStatus = formStatus.Loaded _
And Me._sourceStatus = sourceStatus.POSSalesLine _
And currentSourcePosition <> POSSALESTABLEPOSSALESLINEBindingSource.Position
Then
Dim _itemId As String = POSSALESLINEDataGridView.CurrentRow.Cells(1).Value
Dim _lineNum As Integer = POSSALESLINEDataGridView.CurrentRow.Cells(0).Value
Dim _fileName As String
_fileName = findPicPath(_itemId)
picItem = LoadImage(picItem, _fileName) currentSourcePosition = POSSALESTABLEPOSSALESLINEBindingSource.Position End If End Sub
使用Bindingsource_CurrentChange的方法
Private Sub InventTableBindingSource_CurrentChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Handles InventTableBindingSource.CurrentChanged
If _formStatus = formStatus.Loaded Then
Dim _itemId As String = InventTableMyDataGridView.CurrentRow.Cells(0).Value
Dim _position As Integer = InventTableBindingSource.Position
Dim _itemId2 As String = InventTableBindingSource.Current(1).ToString()
MessageBox.Show("ItemId:" & _itemId & " - ItemId 2: " & _itemId2 & " - position:" & _position)
End If End Sub