context menu for Selected row in radgrid

4 posts, 0 answers
  1. Mohan
    Mohan avatar
    11 posts
    Member since:
    Jul 2012

    Posted 15 Jul 2012 Link to this post

        How to show the Context Menu only when right click the mouse on the selected row in rad grid.
        Please give solution for this......

  2. Stefan
    Stefan avatar
    2911 posts

    Posted 17 Jul 2012 Link to this post

    Hi Mohan,

    Thank you for writing.

    When you right click a row in RadGridView it will automatically be selected, so your condition will always be satisfied and the context menu will always be opened. However, for your convenience, here is how to access the clicked row and its properties while opening the context menu:
    void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
        GridDataCellElement dataCell = e.ContextMenuProvider as GridDataCellElement;
        if (dataCell != null)
            e.Cancel = !dataCell.RowInfo.IsSelected;

    I hope that you find this information useful.
    All the best,
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
  3. Curtis
    Curtis avatar
    16 posts
    Member since:
    Nov 2010

    Posted 10 Sep 2013 Link to this post

    Hey there,

    Wanted to thank you for including this code snippet - it took a little bit to find the solution for my issue.

    Although the row is selected upon the right-click action, there are still cases where you need to use the code provided. An example is when you only want to modify the context menu for a given row type (i.e. you only want to modify the context menu for data rows and not filter/header rows). With this code, I was able to compare the type of e.ContextMenuProvider to only modify the context menu for data rows. This could easily be modified to be able to only modify context menus for other types such as header rows, filter rows and new rows.

    Dim menuItem As RadMenuItem = Nothing
    Dim menuSeperator As RadMenuSeparatorItem = Nothing
    ' modify the context menu of grid view data rows (i.e. not the header/filter/etc. rows)
    If TypeOf e.ContextMenuProvider Is GridDataCellElement Then
        If e.ContextMenu.Items.Count > 0 Then
            menuSeperator = New RadMenuSeparatorItem
        End If
        menuItem = New RadMenuItem("View BOM")
        AddHandler menuItem.Click, AddressOf OnContextMenuClicked_ViewBom
        menuItem = New RadMenuItem("Edit BOM")
        AddHandler menuItem.Click, AddressOf OnContextMenuClicked_EditBom
    End If

    Thanks again!
  4. Stefan
    Stefan avatar
    2911 posts

    Posted 12 Sep 2013 Link to this post

    You are most welcome. Let us know if you have any other questions.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
Back to Top