This documentation is archived and is not being maintained.

DataGrid.HitTest Method (Int32, Int32)

.NET Framework 1.1

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.

[Visual Basic]
Overloads Public Function HitTest( _
   ByVal x As Integer, _
   ByVal y As Integer _
) As HitTestInfo
[C#]
public HitTestInfo HitTest(
   int x,
 int y
);
[C++]
public: HitTestInfo* HitTest(
   int x,
 int y
);
[JScript]
public function HitTest(
   x : int,
 y : int
) : HitTestInfo;

Parameters

x
The horizontal position of the coordinate.
y
The vertical position of the coordinate.

Return Value

A DataGrid.HitTestInfo that contains information about the clicked part of the grid.

Remarks

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 both 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.

Example

[Visual Basic] The following example uses the HitTest method in an event that occurs when the user clicks in the grid.

[Visual Basic] 
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
 

[C#, C++, JScript] No example is available for C#, C++, or JScript. To view a Visual Basic example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

DataGrid Class | DataGrid Members | System.Windows.Forms Namespace | DataGrid.HitTest Overload List | MouseDown

Show: