DataGrid.HitTest 方法

定义

获取屏幕上指定点的 DataGrid 控件的信息。

重载

HitTest(Int32, Int32)

使用传递给方法的 x 和 y 坐标获取信息(如网格上被单击点的行号和列号)。

HitTest(Point)

获取有关使用特定 Point 的网格的信息(如网格中被单击点的行号和列号)。

HitTest(Int32, Int32)

使用传递给方法的 x 和 y 坐标获取信息(如网格上被单击点的行号和列号)。

C#
public System.Windows.Forms.DataGrid.HitTestInfo HitTest(int x, int y);

参数

x
Int32

坐标的水平位置。

y
Int32

坐标的垂直位置。

返回

一个 DataGrid.HitTestInfo,它包含有关网格中被单击部分的信息。

示例

下面的代码示例在用户单击网格时发生的事件中使用 HitTest 方法。

VB
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

注解

DataGrid.HitTestInfoHitTest 控件的 System.Windows.Forms.DataGrid 方法结合使用,用于确定用户已单击控件的System.Windows.Forms.DataGrid哪个部分。 包含 DataGrid.HitTestInfo 所单击的网格的行、列和部分。 此外, Type 属性返回 DataGrid.HitTestType 枚举。

方法HitTest采用控件的 DragDropMouseMoveDragEnterMouseDownDragOverMouseUp 和 事件提供的 System.Windows.Forms.DataGrid x 和 MouseWheel y 参数。

另请参阅

适用于

.NET Framework 4.8.1 和其他版本
产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 10

HitTest(Point)

获取有关使用特定 Point 的网格的信息(如网格中被单击点的行号和列号)。

C#
public System.Windows.Forms.DataGrid.HitTestInfo HitTest(System.Drawing.Point position);

参数

position
Point

一个 Point,它代表单个 x,y 坐标。

返回

一个 DataGrid.HitTestInfo,它包含有关该网格的特定信息。

示例

下面的代码示例使用 HitTest 中的 方法,当用户单击网格时发生。

VB
Private Sub DataGrid1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
    Dim hti As DataGrid.HitTestInfo
    hti = grid.HitTest(New Point(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

注解

DataGrid.HitTestInfoHitTest 控件的 System.Windows.Forms.DataGrid 方法结合使用,用于确定用户已单击控件的System.Windows.Forms.DataGrid哪个部分。 包含 DataGrid.HitTestInfo 所单击的网格的行、列和部分。 此外, Type 属性返回 DataGrid.HitTestType 枚举。

方法HitTest采用控件的 DragDropMouseMoveDragEnterMouseDownDragOverMouseUp 和 事件提供的 System.Windows.Forms.DataGrid x 和 MouseWheel y 参数。

另请参阅

适用于

.NET Framework 4.8.1 和其他版本
产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 10