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

How to modify GridView cell's right click menu?

4 Answers 439 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Onur
Top achievements
Rank 1
Onur asked on 17 Apr 2015, 01:21 PM

Hi,

I'm using gridview to list some important datas. After datas are listed, when i right clicked on the cell's, it shows a standard menu that contains copy, paste, cut, delete and some another options. But, i don't want that end users can cut or delete the cell's content even accidentally. I need only copy option. So, how can i remove that options except copy from that menu?   

4 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 17 Apr 2015, 02:53 PM
Hello Onur,

Thank you for writing.

You can refer to the GridView >> Modifying the Default Context Menu help article. However, according to the screenshot it seems that this is the context menu shown for the editor. Most of our editors internally host the standard TextBox control, thus the context menu that you are seeing is the standard TextBox context menu. Here is a sample code snippet demonstrating how to customize it in order to display only the "Copy" option:
ContextMenu menu = new System.Windows.Forms.ContextMenu();
 
public Form1()
{
    InitializeComponent();
 
    MenuItem item = new MenuItem();
    item.Click += item_Click;
    item.Text = "Copy";
    menu.MenuItems.Add(item);
}
 
private void item_Click(object sender, EventArgs e)
{
    RadTextBoxEditor tb = this.radGridView1.ActiveEditor as RadTextBoxEditor;
    if (tb != null)
    {
        RadTextBoxEditorElement el = tb.EditorElement as RadTextBoxEditorElement;
 
        ((TextBoxBase)el.TextBoxItem.HostedControl).Copy();
    }
}
 
private void radGridView1_CellEditorInitialized(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
{
    RadTextBoxEditor tb = e.ActiveEditor as RadTextBoxEditor;
    if (tb != null)
    {
        RadTextBoxEditorElement el = tb.EditorElement as RadTextBoxEditorElement;
        
        el.TextBoxItem.HostedControl.ContextMenu = menu;
    }
}

I hope this information helps. Should you have further questions, I would be glad to help.
 
Regards,
Dess
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
Onur
Top achievements
Rank 1
answered on 18 Apr 2015, 09:53 AM

Thanks a lot for your quick and clean solution Dess. 

It  worked as i wanted exactly.

Thanks again.

0
Onur
Top achievements
Rank 1
answered on 18 Apr 2015, 10:27 PM

Hi Dess again,

I have another problem about this. Menu is ok, but although menu has no cut or delete options, i can still use cut and delete options over ctrl+x and del buttons. Is there any ways to disable these controls? Because in my project, cells must be non-editable completely.

Sorry to disturb you again, but if you may help me, i will be appreciate.

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 21 Apr 2015, 02:39 PM
Hello Onur,

Thank you for writing back.

You can disable the paste operation in RadGridView by setting the ClipboardPasteMode property to GridViewClipboardPasteMode.Disable. Additionally, in order to prevent the cut operation you can create a custom MasterGridViewTemplate and override its Cut method where you will stop the base logic.
public class CustomGrid : RadGridView
{
    protected override RadGridViewElement CreateGridViewElement()
    {
        return new CustomRadGridViewElement();
    }
 
    public override string ThemeClassName
    {
        get
        {
            return typeof(RadGridView).FullName;
        }
    }
}
 
public class CustomRadGridViewElement : RadGridViewElement
{
    protected override MasterGridViewTemplate CreateTemplate()
    {
        return new CustomMasterGridViewTemplate();
    }
     
    protected override Type ThemeEffectiveType  
    {
        get 
        {
            return typeof(RadGridViewElement);  
        }
    }
}
 
public class CustomMasterGridViewTemplate : MasterGridViewTemplate
{
    public override void Cut()
    {
    }
}

Note that RadGridView uses row behaviors to handle the keyboard support. In order to prevent a certain key to perform its action you can refer to the Row behaviors help article which demonstrates a sample approach. 

I hope this information helps. If you have any additional questions, please let me know.

Regards,
Dess
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
Tags
GridView
Asked by
Onur
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Onur
Top achievements
Rank 1
Share this question
or