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

ContextMenu Opening without row change

2 Answers 17 Views
GridView
This is a migrated thread and some comments may be shown as answers.
pierre-jean
Top achievements
Rank 1
Veteran
Iron
pierre-jean asked on 13 Apr 2021, 04:55 PM

Hello

I need to open the contextmenu of a radgridview without having the current row changed.

Can I identify if the right mouse button is clicked and cancel the current row changing

or do I need to open the menu from the MouseUp event, in which case I need to identif the row on which the right mouse is click event is fired

Thanks in advance

Best Regards

2 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 14 Apr 2021, 10:56 AM
Hello, Pierre-Jean, 

By default, when you click with the right mouse button, the current row in RadGridView is expected to be changed. However, the CurrentRowChanging event allows you to cancel this operation according to your needs:
        public RadForm1()
        {
            InitializeComponent();

            for (int i = 0; i < 10; i++)
            {
                this.radGridView1.Rows.Add(i, "Data" + i);
            }

            this.radGridView1.CurrentRowChanging+=radGridView1_CurrentRowChanging;
            this.radGridView1.MouseDown+=radGridView1_MouseDown;
            this.radGridView1.MouseUp+=radGridView1_MouseUp;
        }

        bool isRightButton = false;
        private void radGridView1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button== System.Windows.Forms.MouseButtons.Right)
            {
                isRightButton = false;
            }
        }

        private void radGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button== System.Windows.Forms.MouseButtons.Right)
            {
                isRightButton = true;
            }
        }

        private void radGridView1_CurrentRowChanging(object sender, CurrentRowChangingEventArgs e)
        {
            e.Cancel = isRightButton;
        }

However, please have in mind that any further operation from the context menu items, e.g. copy/paste etc. considers the current cell. Hence, if the CurrentRowChanging event is cancelled, the context menu will be shown in the context of the current cell/row which may lead to undesired behavior.

An alternative approach is to show programmatically the context menu for the clicked cell:

        public RadForm1()
        {
            InitializeComponent();

            for (int i = 0; i < 10; i++)
            {
                this.radGridView1.Rows.Add(i, "Data" + i);
            }

            this.radGridView1.CurrentRowChanging+=radGridView1_CurrentRowChanging;
            this.radGridView1.MouseDown+=radGridView1_MouseDown;
            this.radGridView1.MouseUp+=radGridView1_MouseUp;
            this.radGridView1.ContextMenuOpening+=radGridView1_ContextMenuOpening;
        }

        private void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
        {
             e.Cancel = isRightButton;
        }

        bool isRightButton = false;
        private void radGridView1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button== System.Windows.Forms.MouseButtons.Right)
            {
                isRightButton = false;
                GridDataCellElement cell = this.radGridView1.ElementTree.GetElementAtPoint(e.Location) as GridDataCellElement;
                if (cell!=null)
                { 
                    this.radGridView1.GridViewElement.ContextMenuManager.ShowContextMenu(cell);
                }
            }
        }

        private void radGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button== System.Windows.Forms.MouseButtons.Right)
            {
                isRightButton = true;
            }
        }

        private void radGridView1_CurrentRowChanging(object sender, CurrentRowChangingEventArgs e)
        {
            e.Cancel = isRightButton;
        }

But it would be necessary to further manage the copy/paste operations since they also consider the current cell as showing the context menu.

In case you are still experiencing any further difficulties, it would be greatly appreciated you can provide more details about the exact goal that you are trying to achieve. Thus, we would be able to think about a suitable solution and provide further assistance. Thank you in advance.

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Тhe web is about to get a bit better! 

The Progress Hack-For-Good Challenge has started. Learn how to enter and make the web a worthier place: https://progress-worthyweb.devpost.com.

0
pierre-jean
Top achievements
Rank 1
Veteran
Iron
answered on 16 Apr 2021, 01:12 PM

Hello Dess

thanks a lot for your recommendation that works perfectly for me.

I have used the first option

Best regards

Pierre-Jean

Tags
GridView
Asked by
pierre-jean
Top achievements
Rank 1
Veteran
Iron
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
pierre-jean
Top achievements
Rank 1
Veteran
Iron
Share this question
or