在DataGridView列标题上检测鼠标

时间:2012-07-19 17:59:08

标签: vb.net winforms datagridview

我正在尝试使用DataGridView_CellMouseEnter事件来确定鼠标是否超过其中一个列标题,但我无法弄明白。

Private Sub DataGridView_CellMouseEnter(sender As Object, _
                                        e As DataGridViewCellEventArgs) _
                                        Handles DataGridView.CellMouseEnter
  If (StrComp(e.ColumnIndex.ToString, "1")) Then
    Me.Cursor = Cursors.WaitCursor
  Else
    Me.Cursor = Cursors.IBeam
  End If
End Sub

如果有人可以帮助我,我会非常感激。

2 个答案:

答案 0 :(得分:3)

RowIndex = -1应该告诉你:

Private Sub DataGridView1_CellMouseEnter(ByVal sender As Object, _
                                         ByVal e As DataGridViewCellEventArgs) _
                                         Handles DataGridView1.CellMouseEnter
  If e.RowIndex = -1 And e.ColumnIndex > -1 Then
    MessageBox.Show("Over " & DataGridView1.Columns(e.ColumnIndex).HeaderText)
  End If
End Sub

答案 1 :(得分:1)

查看DataGridView.HitTest method

Private Sub dataGridView1_MouseEnter(ByVal sender as Object, ByVal e as EventArgs) Handles dataGridView1.MouseEnter
  Dim relativeMousePosition as Point = dataGridView1.PointToClient(Cursor.Position)
  Dim hit As DataGridView.HitTestInfo = dataGridView1.HitTest(relativeMousePosition.X, relativeMousePosition.Y)
  Console.WriteLine(hit.Type.ToString())
End Sub