KB Article A1386
Visible to All Users

How to show a context menu for grid rows

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 Basic
Private 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

See also: Implementing Custom Behavior for Popup Menus

Show previous comments (6)
DevExpress Support Team 11 years ago

    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)

      Alisher (DevExpress Support) 9 years ago

        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.

        Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

        Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.