Get GridViewColumn on mouse position after right click

1 Answer 49 Views
GridView
alex
Top achievements
Rank 2
Bronze
Iron
Iron
alex asked on 14 Feb 2023, 05:43 PM

Hello,

I am trying to get the GridViewDataColumn after mouse click.

I can notice that radGridView.CurrentCell.Column is updated on left click. So if I perform left click and then right click, the context menu is open and I have reference to the correct column. 

But if Ido right click in different cells, the current cell is not updated al I have refence to the old column.

How can I get the GridViewColumn from Cursor position.

I have only the grid, I haven't any mouse arguments.

1 Answer, 1 is accepted

Sort by
0
Accepted
Stenly
Telerik team
answered on 16 Feb 2023, 09:29 AM

Hello Alex,

To retrieve the GridViewDataColumn column of the GridViewCell element that is clicked with the right mouse button, you could utilize the Mouse.DirectlyOver property, as well as the ParentOfType extension method.

The following code snippet shows this suggestion's implementation inside an event handler for the MouseRightButtonDown event, to which a RadGridView instance is subscribed to:

this.radGridView.MouseRightButtonDown += RadGridView_MouseRightButtonDown;

private void RadGridView_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
    IInputElement element = Mouse.DirectlyOver;

    if (element != null)
    {
        FrameworkElement frameworkElement = element as FrameworkElement;
        
        if (frameworkElement != null)
        {
            GridViewCell parentCell = frameworkElement.ParentOfType<GridViewCell>();

            if (parentCell != null)
            {
                MessageBox.Show(parentCell.Column.Header.ToString());
            }
        }
    }
}

In addition, I have attached a sample project for you to test, which contains the implementation of the above suggestion.

With this being said, I hope the provided information will be of help to you.

Regards,
Stenly
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

alex
Top achievements
Rank 2
Bronze
Iron
Iron
commented on 21 Feb 2023, 06:02 PM

Thank you Stenly for your detailed response.

The problem is that I am not using the right click event at all.

I have an attached property that selects the row before a context menu is open (to overcome default behavior, the user complains if he should click left click to select a eow and then right click to open the menu).

Then I have a service with the reference to the RadGridView. The property CurrentCell/CurrentColumn is not the expected because somehow the row was seleted but the CurrentCell property didn't changed (it seems thay you update it on left click only).

How can I overcome this particular behavior?

I expected that CurrentCell is updated when the row is changed or when the right click is performed. 

I can't set the current cell on right click in my behavior because it is read only.

I wanted somehow to take the mouse position related to the primary screen and get the column. 

All the functions with the TreeView or pointToScreen didn't help.

Stenly
Telerik team
commented on 24 Feb 2023, 12:43 PM

Hello Alex,

I am not 100 percent sure how exactly the row is being selected on your end, however, if it's done via the mouse, you could retrieve the element that it is currently over via the Mouse.DirectlyOver property. On this element, you could search its visual tree upwards (using the ParentOfType extension method on it), in order to find its parent GridViewCell element, which will contain information about its column.

If this suggestion is not applicable on your end, would it be possible to share what action the context menu is being opened and how the row is being selected?

Tags
GridView
Asked by
alex
Top achievements
Rank 2
Bronze
Iron
Iron
Answers by
Stenly
Telerik team
Share this question
or