Get the DataGrid row or column that was clicked

You can use the DataGrid's HitTest method, passing it a point in the grid's client coordinate system, and returning a HitTestInfo object that holds all the row and column information.

    '
    ' X and Y are in the grid's coordinates. If they are in
    ' screen coordinates, call DataGrid.PointToClient method.  	
    '
    Dim pt = New Point(X, Y)  	
    Dim hti As DataGrid.HitTestInfo = Me.DataGrid.HitTest(pt)  	

    If hti.Type = DataGrid.HitTestType.Cell Then  	
        MessageBox.Show(DataGrid(hti.Row, hti.Column).ToString())  	
    Else  	
        If hti.Type = DataGrid.HitTestType.ColumnHeader Then 
          '
          ' Assumes the DataSource is a DataView.  	
          '
          MessageBox.Show(CType(Me.DataGrid.DataSource,  _
              DataView).Table.Columns(hti.Column).ToString())  	
        End If  	
    End If 	



About TheScarms
About TheScarms


Sample code
version info

If you use this code, please mention "www.TheScarms.com"

Email this page


© Copyright 2024 TheScarms
Goto top of page