Live Chat Icon For mobile
Live Chat Icon

How can I control the cursor over my DataGrid

Platform: WinForms| Category: Datagrid

One way you can do this is to derive the DataGrid and override its WndProc method to handle the WM_SETCURSOR method yourself. In your override, you can do hit testing to decide when you want to set the cursor, or when you want to call the base class and let it set the cursor. Here are sample projects (VB and C#) showing the technique.

Public Class MyDataGrid
  Inherits DataGrid
  Private Const WM_SETCURSOR As Integer = 32

  Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    If m.Msg <> WM_SETCURSOR Then
      MyBase.WndProc(m)
    Else
      ’see if you want the cursor - in col 1, rows 2, 3, 4
      Dim pt As Point = Me.PointToClient(Control.MousePosition)
      Dim hti As DataGrid.HitTestInfo = Me.HitTest(pt.X, pt.Y)
      If hti.Column = 1 AndAlso hti.Row > 1 AndAlso hti.Row < 5 Then
          Cursor.Current = Cursors.Hand
        ’if not, call the baseclass
       Else
        MyBase.WndProc(m)
      End If
    End If
  End Sub ’WndProc

  Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
    If Cursor.Current.Equals(Cursors.Hand) Then
      MessageBox.Show(''My MouseDown'')
    Else
      MyBase.OnClick(e)
    End If
  End Sub ’OnMouseDown
End Class ’MyDataGrid

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.