Description:
I wish to display a popup menu for the rows in my grid. However, I've noticed that a row, which is clicked with the right mouse button, does not get focused. Could you please provide some sample code on how to implement a custom context menu for grid rows?
Answer:
The XtraGrid does not focus a row by a right-click. You can do this programmatically by setting the FocusedRowHandle property of a GridView. Most likely, you will be using the
GridView.PopupMenuShowing Event(GridView.ShowGridMenu for versions earlier than v10.2) to display a popup menu. Below is a sample code.
C#private void gridView1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
if (e.HitInfo.InRow) {
GridView view = sender as GridView;
view.FocusedRowHandle = e.HitInfo.RowHandle;
if (radioGroup1.EditValue.ToString() == "Standard Menu")
ContextMenu1.Show(view.GridControl, e.Point);
if (radioGroup1.EditValue.ToString() == "DevExpress Menu") {
foreach (DXMenuItem item in menuItems)
e.Menu.Items.Add(item);
}
}
}
Visual BasicPrivate Sub gridView1_PopupMenuShowing(ByVal sender As Object, ByVal e As PopupMenuShowingEventArgs)
If e.HitInfo.InRow Then
Dim view As GridView = TryCast(sender, GridView)
view.FocusedRowHandle = e.HitInfo.RowHandle
If radioGroup1.EditValue.ToString() = "Standard Menu" Then
ContextMenu1.Show(view.GridControl, e.Point)
End If
If radioGroup1.EditValue.ToString() = "DevExpress Menu" Then
For Each item As DXMenuItem In menuItems
e.Menu.Items.Add(item)
Next item
End If
End If
End Sub
You mention about GridView.ShowGridMenu (GridView.PopupMenuShowing Event in version 10.2) event but you use ShowGridMenu(which I could not find). I also see that this ticket is for 9 years around, is there a newer solution?
We replaced the ShowGridMenu event with the PopupMenuShowing event, starting with version 10.2. I have corrected this article. Although this article is nine years old, it is still relevant.
This example works but doesn't work. The check in code for "HitInfo.InRow" does work, but my context menu still shows whether this evaluates to True or False. Even if I create a "False" condition and do a ContextMenu.Hide(), my context menu still shows, which is undesirable because then I have an object that is nothing because a data row cannot be found in my underlying dataset with a negative primary key value (because the value of HitInfo.RowHandle is negative). Do I need to manually assign and remove assignment of the context menu from the GridControl in order to prevent it from showing when I have a negative HitInfo.RowHandle? Please advise.
Furthermore, removing the line of code ContextMenu1.Show(View.GridControl, e.Point) has no affect on the showing or not showing of the context menu. It still shows no matter what, mainly because I think that it has been assigned to the GridControl so it is going to show whenever a "right click" occurs on the GridControl itself. How do I prevent it from showing when the HitInfo is not over a data row because it seems like I have no control to prevent the context menu from showing since it is assigned at design time as tied to the GridControl?
Hello,
I have checked theHow to show a context menu for grid rows example, and on my side it works properly. For instance, when I comment the call of the ContextMenu.Show method. Please check the attached video and let me know if I missed some steps to reproduce the issue.
I hope to hear from you soon.
Issue resolved. I was setting the ContextMenuStrip property on the DX GridControl so it was overriding the grid contextmenu. Removing that property on the DX grid results in desired functionality.
I am glad to hear that you resolved the issue. Please feel free contact us in the future.
Please show me how to popup the menu when right click on EMPTY area on a grid. (e.HitInfo haven't property for Empty erea)
Hello,
I've created a separate ticket on your behalf (T373755: How to popup the menu when right click on EMPTY area on a grid). It has been placed in our processing queue and will be answered shortly.