This is a migrated thread and some comments may be shown as answers.

context menu for Selected row in radgrid

3 Answers 864 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mohan
Top achievements
Rank 1
Mohan asked on 15 Jul 2012, 09:49 AM
Hi,
    How to show the Context Menu only when right click the mouse on the selected row in rad grid.
    Please give solution for this......

3 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 17 Jul 2012, 12:48 PM
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,
Stefan
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
0
Curtis
Top achievements
Rank 2
answered on 10 Sep 2013, 03:11 PM
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
        e.ContextMenu.Items.Add(menuSeperator)
    End If
 
    menuItem = New RadMenuItem("View BOM")
    AddHandler menuItem.Click, AddressOf OnContextMenuClicked_ViewBom
    e.ContextMenu.Items.Add(menuItem)
 
    menuItem = New RadMenuItem("Edit BOM")
    AddHandler menuItem.Click, AddressOf OnContextMenuClicked_EditBom
    e.ContextMenu.Items.Add(menuItem)
 
End If

Thanks again!
0
Stefan
Telerik team
answered on 12 Sep 2013, 11:18 AM
You are most welcome. Let us know if you have any other questions.
 
Regards,
Stefan
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
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 >>
Tags
GridView
Asked by
Mohan
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Curtis
Top achievements
Rank 2
Share this question
or