简体   繁体   中英

Detect mouse over DataGridView Column Header

I am trying to use the DataGridView_CellMouseEnter event to figure out if the mouse is over one of the column headers, but I can't figure it out.

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

If someone could help me I would really appreciate it.

RowIndex = -1 should tell you that:

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

Have a look at the 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM