为什么在DATAGRID上点击右键返回的记录不是当前光标所在的行的记录
本帖最后由 bcrun 于 2013-07-30 11:25:49 编辑 代码如下:
Private Sub mnuDayinPJ3_Click()
On Error GoTo Errc
Call DayinPiaoju(sdrbb.Columns("用户编号"), Val(sdrbb.Columns("操作次数")))
Exit Sub
Errc:
MsgBox ERR.Description
End Sub
Private Sub sdrbb_MouseUp(Button As Integer, Shift As Integer, X As Single, y As Single)
On Error GoTo Errc
'实现右键datagrid上被点击的行为当前行
Dim RowValue, ColValue
RowValue = sdrbb.RowContaining(y)
ColValue = sdrbb.ColContaining(X)
If RowValue >= 0 And RowValue <= sdrbb.VisibleRows And _
ColValue >= 0 And ColValue <= sdrbb.VisibleCols Then
sdrbb.Row = RowValue
End If
Adodc1.Recordset.MoveFirst
If RowValue < 0 Then Exit Sub
Adodc1.Recordset.Move RowValue
If Button = 2 Then '如果是鼠标右键按下时
Me.PopupMenu mnuDayinPJ2, 0, X + sdrbb.Left, y + sdrbb.Top '弹出右键菜单
End If
Exit Sub
Errc:
MsgBox ERR.Description
End Sub
表中有1300行,当我在DATAGRID上察看第1000行记录时,在其上点击右键,而sdrbb.Columns("用户编号"), Val(sdrbb.Columns("操作次数") 返回的均是 DATAGRID中第7行的记录,
请问是何原因。谢谢大家。
[解决办法]
在 DataGrid 的 RowColChange 事件中执行代码。并且在 ADODC.Recordset 中直接访问记录字段。
------解决方案--------------------
DataGrid 的行、列号是基于当前显示的可见行列的,这一点与 FlexGrid 等不同。最好不要用这些参数来访问记录。
既然 DataGrid 绑定记录集,直接访问记录集即可。在 RowColChange 事件后,当前记录就是光标所在。