How to use a context menu on DataGrid?? Can't get row.

If you hold-click on a Data Grid to pop-up the Context menu you're obviously wanting options based on the item that was clicked on. I've found no way what-so-ever to determine what row was clicked on since the Mouse Down event doesn't fire on a hold-click. How in the world can you do provide Context specific menus on a Data Grid Something simple like Delete Row in a context menu.


Answer this question

How to use a context menu on DataGrid?? Can't get row.

  • Gentlehag

    But DataGrid doesn't fires MouseUp/MouseDown event for right-click if no ContextMenus attached to grid.
    How can I detect right-click if no context-menu specified


  • Grace SQL

    Use the context menu popup event and calculate the position of the row as follows

    Dim pt As Point
    pt = dg.PointToClient(Control.MousePosition)
    Dim ht As DataGrid.HitTestInfo = dg.HitTest(pt.X, pt.Y)
    dg.Select(hti.Row)
    dg.CurrentRowIndex = hti.Row

  • TheMaj0r

    There's the DataGrid.HitTest method that you can use. Get the screen coordinates using Control.MousePosition.

    HTH.. Alex


  • DaPosh

    Well, the problem is that the Context Menu Item that is clicked on might not be over the Row that initially launched the Context Menu. For instance, using HitTest with Control.MousePosition with a Context Menu that has several items in it will not return the correct row for any but the first (likely). The menu event is fired when the menu item is clicked and THIS is now the Control.MousePosition, thus you may very likely be off the row. Is ANYONE actually using a context menu with a control that lists various lines of data This seems like a pretty common usage of a Context menu. In the desktop framework, the MouseDown event fires so you can get the proper row. I can't find any way to do this in the CF.

  • hellomcv

    Alex, you were right. My confusion arose from the fact that the Control.MousePosition is relative to the form not the control and must be translated with PointToClient. Thanks!

  • How to use a context menu on DataGrid?? Can't get row.