我给一个名为sdrbb的DATAGRID控件添加右键菜单后,无法获取到鼠标所点击的DATAGRID的当前行的数据。请大家帮忙看一下是怎么回事。代码如下:Private Sub sdrbb_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
On Error GoTo Errc
  If Button = 2 Then                    
      Me.PopupMenu mnuDayinPJ, 0, X, Y    '弹出右键菜单  一级菜单
  End If
Exit Sub
Errc:
  MsgBox ERR.Description
End Sub
Private Sub mnuDayinPJ1_Click()   '一级菜单下的子菜单
    Call DayinPiaoju(sdrbb.Columns("用户编号").value,sdrbb.Columns("购物次数").value)
End Sub具体的错误提示是:  错误代码6160   数据访问错误。

解决方案 »

  1.   

    好象是错在,我在DATAGRID上点击了右键之后,datagrid上被点击的行并不能成为当前行。无从获取数据,所以出现错误,现在的问题就是,如何让鼠标点击的行成为当前行,或被选中的行。
      

  2.   

    DATAGRID本没有这个功能:在DATAGRID上鼠标右键,记录并不会自动定位到当前记录位置.通过变通的方法可以实现,加入我的代码,rs为绑定的数据集,如果为adodc控件就代替rs即可,DataGrid1是DataGrid控件Private Sub DataGrid1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        DataGrid1.SetFocus
    End SubPrivate Sub DataGrid1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Dim i As Long
        i = Int(Y / DataGrid1.RowHeight) - 1
        rs.MoveFirst
        rs.Move i
    End Sub
      

  3.   


    首先谢谢你,而后有两个问题:1、被选中的单元格的内容老是闪烁,估计和DataGrid1.SetFocus不停的被执行有关,如何判断当前有点的控件是DATAGRID1,就不执行DataGrid1.SetFocus2、右键菜单出现的位置不是我的鼠标点击的地方,好象右键菜单出现的地方的与鼠标点击的地方 仅坐标X是一致的,但坐标Y不一致,如何解决。
      

  4.   

    1、被选中的单元格的内容老是闪烁,估计和DataGrid1.SetFocus不停的被执行有关,如何判断当前有点的控件是DATAGRID1,就不执行DataGrid1.SetFocus如何判断当前有焦点的控件,少了个字。
      

  5.   

    if typeof me.ActiveControl is DataGrid1 Then
      

  6.   

    回答:
    1.将DataGrid1_MouseMove事件换成DataGrid1_MouseDown即可解决
    2.在DataGrid1_MouseUp里增加一句输出,看位置错到哪?
      估计在+1 to -1之间,如果是就改一下语句,如
      i = Int(Y / DataGrid1.RowHeight) '- 1
    -1注释了,这个需要自己判定一下
      

  7.   

    给一个更合理方法,去掉DataGrid1_MouseMove或DataGrid1_MouseDown方法Private Sub DataGrid1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        '实现右键datagrid上被点击的行为当前行
        Dim RowValue, ColValue
        RowValue = DataGrid1.RowContaining(Y)
        ColValue = DataGrid1.ColContaining(X)
        
        If RowValue >= 0 And RowValue <= DataGrid1.VisibleRows And _
            ColValue >= 0 And ColValue <= DataGrid1.VisibleCols Then
            DataGrid1.Row = RowValue
        End If
    End Sub
      

  8.   

    综合使用 toprow,cellleft,celltop,cellheight,cellwidth