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

[Solved] Adding a context menu to a programmatically created grid

2 Answers 294 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michael Dunbar
Top achievements
Rank 2
Michael Dunbar asked on 11 May 2009, 09:26 AM
How do you do it?

I currently have:

RadGrid gridFileView = new RadGrid(); 
            gridFileView.ID = gridFileViewID
            gridFileView.EnableEmbeddedSkins = false
            gridFileView.Skin = "FileExplorer"
            gridFileView.AllowPaging = true
            gridFileView.PageSize = 20
            gridFileView.AllowSorting = true
            gridFileView.ClientSettings.AllowRowsDragDrop = true
            gridFileView.ClientSettings.Selecting.AllowRowSelect = true
            gridFileView.ItemDataBound += new GridItemEventHandler(gridFileView_ItemDataBound); 
            gridFileView.RowDrop += new GridDragDropEventHandler(gridFileView_RowDrop); 
            pnlFileView.Controls.Add(gridFileView); 
 
            AddExistingFiles(gridFileView, docs); 
 
            // Add file view context menu 
            ContextMenuControlTarget target = new ContextMenuControlTarget(); 
            target.ControlID = gridFileViewID
 
            RadContextMenu cmFileView = new RadContextMenu(); 
            cmFileView.Targets.Add(target); 
            AddFileViewContextMenuItems(cmFileView); 

I've added a few test menu items in the AddFileViewContextMenuItems function but the menu doesn't appear when I right click over the grid. Do I need to call the context menu with an event on the grid itself?

2 Answers, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 12 May 2009, 10:01 AM
Hello Michael,

I have answered to the support thread open on this matter.
Here is a quote of the answer:

Hello Michael,

Thank you for contacting us.

To achieve your goal you need to wire the
OnRowContextMenu client event. In its event handler you just need to find the dynamically added menu and show it on the corresponding row.

Here is a code snippet showing how to achieve this:

[C#]
newGrid.ClientSettings.ClientEvents.OnRowContextMenu = "RowContextMenu"

[JavaScript]
            function RowContextMenu(sender, eventArgs) { 
                var menu = $find('<%= plhRadGrid.FindControl("RadMenu1").ClientID %>'); 
                var evt = eventArgs.get_domEvent(); 
 
                if (evt.target.tagName == "INPUT" || evt.target.tagName == "A") { 
                    return
                } 
 
                var index = eventArgs.get_itemIndexHierarchical(); 
 
                sender.get_masterTableView().selectItem(sender.get_masterTableView().get_dataItems()[index].get_element(), true); 
 
                menu.show(evt); 
 
                evt.cancelBubble = true
                evt.returnValue = false
 
                if (evt.stopPropagation) { 
                    evt.stopPropagation(); 
                    evt.preventDefault(); 
                } 
            } 

Note that plhRadGrid control is actually the place holder for the dynamically added controls.

For your convenience I have attached a test project which implements the required functionality. For further information I will suggest you review this online demo.

Let me know if other questions arise.


If you do not mind we will continue the communication in the started support thread on this issue in order to avoid duplicate discussions. In this way you will be in touch how the resolution of the problem goes.

Best regards,
Georgi Krustev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
ayush mohan
Top achievements
Rank 1
answered on 24 Oct 2009, 06:19 PM
Hi, I need similar help in my case.. Can you please help me
Tags
Grid
Asked by
Michael Dunbar
Top achievements
Rank 2
Answers by
Georgi Krustev
Telerik team
ayush mohan
Top achievements
Rank 1
Share this question
or