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

Enable/Disable context menu

9 Answers 884 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Deepak
Top achievements
Rank 1
Deepak asked on 22 Feb 2011, 03:22 PM
Hi,
I need to enable disable context menu depending on the cell that is currently right clicked on. How do I go about this?

Thanks
Deepak

9 Answers, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 22 Feb 2011, 03:31 PM
Hi Deepak,

Hope you're well. You need to subscribe to the ContextMenuOpening event, and then you can inspect what you need to go about cancelling the event. For exmaple: If you have a column called Id, and you want to stop the context menu for Id 2 then...

private void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
{
    if (this.radGridView1.CurrentRow.Index > -1) // if it's a data row
    
        if (this.radGridView1.CurrentColumn.Name == "Id") // and the column name is...
        {
            if (Convert.ToInt32(this.radGridView1.CurrentCell.Value) == 2)
            {
                e.Cancel = true;
            }
        }            
    }
}

Hope that helps
Richard
0
Deepak
Top achievements
Rank 1
answered on 22 Feb 2011, 05:10 PM
Hi Richard,
 e.cancel does not seem to work. I still get the context menu popping up with e.cancel set to true.

Thanks
Deepak
0
Richard Slade
Top achievements
Rank 2
answered on 22 Feb 2011, 05:15 PM
Hi Deepak,

Couple of quick questions.
Are you using the latest version? Not that this should make much difference I don't think with this event.
Are you sure that the event is being hit? I.e Have you added
this.radGridView1.ContextMenuOpening += new Telerik.WinControls.UI.ContextMenuOpeningEventHandler(this.radGridView1_ContextMenuOpening);

Are you sure the condition in the event is being hit?
Thanks
Richard
0
Deepak
Top achievements
Rank 1
answered on 22 Feb 2011, 05:19 PM
yes it does hit it. I must be doing something silly for it not to obey e.cancel = true
0
Richard Slade
Top achievements
Rank 2
answered on 22 Feb 2011, 05:32 PM
Hi Deepak,

Here is a really basic exmaple to try. It should show you a context menu on A1/B1 but not on A2/B2
Let me know if that works for you
Richard

Designer File
partial class Form1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components;
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }
    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.radGridView1 = new Telerik.WinControls.UI.RadGridView();
        ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit();
        this.SuspendLayout();
        // 
        // radGridView1
        // 
        this.radGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.radGridView1.Location = new System.Drawing.Point(0, 0);
        this.radGridView1.Name = "radGridView1";
        this.radGridView1.Size = new System.Drawing.Size(284, 262);
        this.radGridView1.TabIndex = 0;
        this.radGridView1.Text = "radGridView1";
        this.radGridView1.ContextMenuOpening += new Telerik.WinControls.UI.ContextMenuOpeningEventHandler(this.radGridView1_ContextMenuOpening);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(284, 262);
        this.Controls.Add(this.radGridView1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.Load += new System.EventHandler(this.Form1_Load);
        ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit();
        this.ResumeLayout(false);
    }
    #endregion
    private Telerik.WinControls.UI.RadGridView radGridView1;
}

Form1.cs
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
  
  
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
  
        private void Form1_Load(object sender, EventArgs e)
        {
            this.radGridView1.Columns.Add(new GridViewTextBoxColumn("A"));
            this.radGridView1.Columns.Add(new GridViewTextBoxColumn("B"));
            this.radGridView1.Rows.Add("A1", "B1");
            this.radGridView1.Rows.Add("A2", "B2");
        }
  
        private void radGridView1_ContextMenuOpening(object sender, Telerik.WinControls.UI.ContextMenuOpeningEventArgs e)
        {
            if (this.radGridView1.CurrentRow.Index > -1)
            {
                if (this.radGridView1.CurrentRow.Cells["A"].Value.ToString() == "A2")
                {
                    e.Cancel = true;
                }
            }
        }
    }

Richard
0
Richard Slade
Top achievements
Rank 2
answered on 22 Feb 2011, 05:38 PM
Note, you could also use
e.ContextMenu.Items.Clear();

Richard
0
Deepak
Top achievements
Rank 1
answered on 22 Feb 2011, 05:45 PM
Your code works. However I have added custom menu items and they do not get cancelled in the event

 

 

 

private

 

 

void Form1_Load(object sender, EventArgs e)

 

 

{

 

 

 

this.radGridView1.Columns.Add(new GridViewTextBoxColumn("A"));

 

 

 

 

this.radGridView1.Columns.Add(new GridViewTextBoxColumn("B"));

 

 

 

 

this.radGridView1.Rows.Add("A1", "B1");

 

 

 

 

this.radGridView1.Rows.Add("A2", "B2");

 

 

 

 

this.radGridView1.ContextMenu = new ContextMenu();

 

 

 

 

this.radGridView1.ContextMenu.MenuItems.Add("custom");

 

 

}

 

 

 

 

 

 

 

0
Deepak
Top achievements
Rank 1
answered on 22 Feb 2011, 06:06 PM
Hi Richard,
   I have got around it by using the Popup event of the context menu object. Here depending on the cell, I am either clearing or adding items to the context menu

Thanks
Deepak
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 22 Feb 2011, 06:09 PM
Hi Deepak,

The reason for this is that the context menu you have added is a Windows Context Menu. The e.ContextMenu that we are handling is a RadDropDownMenu.

My advice would be to drop the windows context menu and create the menu item dynamically in the ContextMenuOpening event.

Here is a revised sample

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
  
  
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
  
        private void Form1_Load(object sender, EventArgs e)
        {
            this.radGridView1.AllowCellContextMenu = true;
  
            this.radGridView1.Columns.Add(new GridViewTextBoxColumn("A"));
            this.radGridView1.Columns.Add(new GridViewTextBoxColumn("B"));
            this.radGridView1.Rows.Add("A1", "B1");
            this.radGridView1.Rows.Add("A2", "B2");    
        }
  
  
        private void radGridView1_ContextMenuOpening(object sender, Telerik.WinControls.UI.ContextMenuOpeningEventArgs e)
        {
            if (this.radGridView1.CurrentRow.Index > -1)
            {
                if (this.radGridView1.CurrentRow.Cells["A"].Value.ToString() == "A2")
                {
                    e.Cancel = true;
                }
                else
                {
                    e.ContextMenu.Items.Clear(); // clears the default items
                    e.ContextMenu.Items.Add(new RadMenuItem("Custom"));
                }
            }
        }
  
    }

Hope that helps
Richard
Tags
GridView
Asked by
Deepak
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Deepak
Top achievements
Rank 1
Share this question
or