Export (0) Print
Expand All

DataGrid.HitTest Method (Int32, Int32)

Gets information, such as row and column number of a clicked point on the grid, using the x and y coordinate passed to the method.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public DataGrid.HitTestInfo HitTest(
	int x,
	int y
)

Parameters

x
Type: System.Int32

The horizontal position of the coordinate.

y
Type: System.Int32

The vertical position of the coordinate.

Return Value

Type: System.Windows.Forms.DataGrid.HitTestInfo
A DataGrid.HitTestInfo that contains information about the clicked part of the grid.

The DataGrid.HitTestInfo, in conjunction with the HitTest method of the System.Windows.Forms.DataGrid control, is used to determine which part of a System.Windows.Forms.DataGrid control the user has clicked. The DataGrid.HitTestInfo contains the row, column, and part of the grid that was clicked. Additionally, the Type property returns a DataGrid.HitTestType enumeration.

The HitTest method takes an x and y argument supplied by the System.Windows.Forms.DataGrid control's DragDrop, DragEnter, DragOver, MouseDown, MouseMove, MouseUp and MouseWheel events.

The following code example uses the HitTest method in an event that occurs when the user clicks in the grid.

Private Sub DataGrid1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
    Dim myGrid As DataGrid = CType(sender, DataGrid)
    Dim hti As System.Windows.Forms.DataGrid.HitTestInfo
    hti = myGrid.HitTest(e.X, e.Y)
    Select Case hti.Type
    Case System.Windows.Forms.DataGrid.HitTestType.None 
       Console.WriteLine("You clicked the background.")
    Case System.Windows.Forms.DataGrid.HitTestType.Cell 
       Console.WriteLine("You clicked cell at row " & hti.Row & ", col " & hti.Column)
    Case System.Windows.Forms.DataGrid.HitTestType.ColumnHeader
       Console.WriteLine("You clicked the column header for column " & hti.Column)
    Case System.Windows.Forms.DataGrid.HitTestType.RowHeader 
       Console.WriteLine("You clicked the row header for row " & hti.Row)
    Case System.Windows.Forms.DataGrid.HitTestType.ColumnResize
       Console.WriteLine("You clicked the column resizer for column " & hti.Column)
    Case System.Windows.Forms.DataGrid.HitTestType.RowResize 
       Console.WriteLine("You clicked the row resizer for row " & hti.Row)
    Case System.Windows.Forms.DataGrid.HitTestType.Caption
       Console.WriteLine("You clicked the caption")
    Case System.Windows.Forms.DataGrid.HitTestType.ParentRows 
       Console.WriteLine("You clicked the parent row")
    End Select 

 End Sub
 

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft