How to find the row clicked in a datagrid

  • Thread starter Thread starter Guest
  • Start date Start date
In the MouseUp event for the Datagrid place the following code

miHitRow = dg.CurrentCell.RowNumber


or Call one of the following methods in the Datagrid MouseUp event.


Public Function GetClickedRow(ByVal e As _
System.Windows.Forms.MouseEventArgs, _
ByRef dbg As DataGrid) As Integer
' this method is called from the mouseup event of a datagrid
' returns the clicked row number (zero based)
Try
Dim pt As Point = New Point(e.X, e.Y)
Dim hti As DataGrid.HitTestInfo = dbg.HitTest(pt)
Return hti.Row
Catch ex As System.Exception
' ignore the error if the user clicked outside the grid rows,
' e.g., in the header...
End Try
End Function

Public Sub GetClickedCell(ByRef iRow As Integer, _
ByRef iCol As Integer, _
ByRef dbg As DataGrid, _
ByVal e As System.Windows.Forms.MouseEventArgs)
Try
Dim pt As Point = New Point(e.X, e.Y)
Dim hti As DataGrid.HitTestInfo = dbg.HitTest(pt)

Catch ex As System.Exception
MsgBox(ex.ToString)
End Try

End Sub
HTH
Les Smith
http://www.knowdotnet.com
 
Dim pt As Point = New Point(e.X, e.Y)

Dim hti As DataGrid.HitTestInfo = dbg.HitTest(pt)

If hti.Type = DataGrid.HitTestType.Cell Then

dbg.CurrentCell = New DataGridCell(hti.Row, hti.Column)

HTH

Les Smith

http://www.KnowDotNet.com
 
Back
Top