How to modify GridView cell's right click menu?

5 posts, 0 answers
  1. Onur
    Onur avatar
    6 posts
    Member since:
    Apr 2015

    Posted 17 Apr 2015 Link to this post

    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?   

  2. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 17 Apr 2015 Link to this post

    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.

     
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Onur
    Onur avatar
    6 posts
    Member since:
    Apr 2015

    Posted 18 Apr 2015 in reply to Dess Link to this post

    Thanks a lot for your quick and clean solution Dess. 

    It  worked as i wanted exactly.

    Thanks again.

  5. Onur
    Onur avatar
    6 posts
    Member since:
    Apr 2015

    Posted 18 Apr 2015 in reply to Dess Link to this post

    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.

  6. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 21 Apr 2015 Link to this post

    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.

     
Back to Top
UI for WinForms is Visual Studio 2017 Ready