I have a RadGrid with an associated RadContextMenu. When I right-click a row on the grid and click one of the context menu items I need to get at the selected row in the context menu's event handlers. However, I found that the SelectedItems collection is empty.
I was able to work around this by adding an OnRowContextMenu handler, with code like this:
function Grid_OnRowContextMenu(sender, args)
{
var index = args.get_itemIndexHierarchical();
var row = sender.MasterTableView.get_dataItems()[index];
row.set_selected();
}
This partially works, but it seems like there should be an easier way to do this. For one, it doesn't deselect other selected rows. Right now I have to add this client-side handler to every single one of my grids. It seems like there should be some sort of property on the grid that enables row selection on right click.
One other problem I have is that I do not want the context menu to show when the grid has no rows. With the old RadGrid I was able to check the row's ItemType property. If it was "NoRecordsItem", I would not show the context menu.
8 Answers, 1 is accepted
There is no internal mechanism which to select a grid row automatically on right-click, however you can select the relevant record explicitly either with the approach you already discovered or as demonstrated in this integration example:
http://www.telerik.com/DEMOS/ASPNET/Prometheus/Controls/Examples/Integration/GridAndMenu/DefaultCS.aspx?product=grid
Concerning your question about the context menu display when there are no records available in the grid:
Consider detecting whether the right-clicked row has data key value available (inside the OnRowContextMenu client event handler) and if such value is not present, avoid calling the show() method of the context menu.
Best regards,
Stephen
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
Given the fact that this is a common need, I highly recommend that you at least add this support to the RadGrid, or better yet, the RadGrid should really have a ContentMenu collection, like the RadTreeView.
What it really needs is the ability to associate a context menu to a grid row, and to associate one with the grid itself. In other words, the row-level context menu would show when the user right clicks on a grid row, and might be used for such commands as Cut and Copy. The non-row-level one should appear when right-clicking in the empty area below the last grid row (assuming that the grid has some white area below it). This could be used for such commands that do not apply to any particular grid row...like Paste.
Thank you for the suggestion.
I will forward your request to our development department for further consideration. If this appears to be a common request and an attainable enhancement, you may see it included in a future version of the product.
Meanwhile you may consider using the solutions provided by my colleague.
Best regards,
Rosen
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
Instead, I have associated the context menu with the grid by setting the grid as a ContextMenuControlTarget.
Thanks,
Kenny
The other is a grid-level context menu. In other words, one that would appear when the user right-clicks in the empty area below the last record in the grid. This is where you would put grid-level items like Paste.
If I wanted to Copy from one grid and Paste into another, I would expect the behavior to be like Windows Explorer. I would right-click on a row in the first grid and select Copy. I would then right-click in an open area (not on a row) in the second grid and select Paste.
This is a very common user interface need, certainly one that I need to implement in the product that I am building with your control set, so I hope that this support will be coming soon in RadGrid. Although I have been able to come up with a less than perfect solution for the row-level context menu (by writing some custom JavaScript), I have yet to find a way to pull off the grid-level (Paste) context menu, and have temporarily had to rely on a Paste button in my toolbar.
Thanks,
Ross
Thanks for the additional feedback and thoughts shared.
All the best,
Rosen
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.