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

Select row with right mouse click

11 Answers 2122 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Roland Vonk
Top achievements
Rank 1
Roland Vonk asked on 03 Jan 2012, 03:27 PM
Hello Telerik,

Any tips on how to enable selection of a GridViewRow from a right mouse click?

There are plenty of examples demostrating how to do this when a ContextMenu is involved, but I do now want a ContextMenu in my case.

Thanks in advance!

Regards,
Roland

11 Answers, 1 is accepted

Sort by
1
Dimitrina
Telerik team
answered on 03 Jan 2012, 04:33 PM
Hello Roland,

You could select a row on a right mouse click after you subscribe for the MouseRightButtonUp event of the RadGridView. Then you should check where you have clicked. If you have clicked on a row, then mark the clicked row as selected.

private void clubsGrid_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
       {
           var s = e.OriginalSource as FrameworkElement;
           if (s is TextBlock)
           {
               var parentRow = s.ParentOfType<GridViewRow>();
               if (parentRow != null)
               {
                   parentRow.IsSelected = true;
               }
           }
       }

I believe that this is what you are looking for.

Regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Roland Vonk
Top achievements
Rank 1
answered on 04 Jan 2012, 09:09 AM
Thank you! That did the trick :-)
1
Fatema
Top achievements
Rank 1
answered on 14 Sep 2012, 06:40 AM
Hello,

Is there any way to do this in MVVM way. There are older post which give solution to attach a behaviour. But their links are not getting open.

Please can you share a attach behaviour to solve this problem in MVVM way.

Thanks,
Fatema
0
Vlad
Telerik team
answered on 14 Sep 2012, 07:02 AM
Hello Fatema,

 Actually our official demo is using attached behavior. You can find the same example in your local copy of our WPF demos as well. 

Regards,
Vlad
the Telerik team

Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

0
Fatema
Top achievements
Rank 1
answered on 14 Sep 2012, 09:22 AM
Hello Vlad,

In the demo context menu is used i want to apply select row on right mouse button click. 
I am sorry but i am not able to find a demo for this.

Regards,
Fatema
0
Vlad
Telerik team
answered on 14 Sep 2012, 09:57 AM
Hello,

 The code is virtually the same. You need however MouseRightButtonUp instead ItemClick of the menu.

All the best,
Vlad
the Telerik team

Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

0
Eric
Top achievements
Rank 1
answered on 09 Oct 2013, 11:11 AM
Implementing the MouseRightButtonUp works fine. However when using the RadGridView with SelectionMode="Extended", the selection doesn't disappear anymore if you rightclick on several rows.
Can I do something about this to make it work, or is this a bug?
0
Dimitrina
Telerik team
answered on 14 Oct 2013, 08:18 AM
Hi Eric,

In this forum thread there is a discussion how to select a row when the user clicks the right mouse button. As I understand you would like to unselect a row. How have you implemented the deselection?
Do you experience the same result if the SelectionMode is Single or Multiple?

Regards,
Didie
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Eric
Top achievements
Rank 1
answered on 15 Oct 2013, 06:36 AM
Hi Didie,

Thanks for your reply. In the meantime I have solved this issue. When using the SelectionMode="Extended" I wasn't aware of the fact that I had to (select and) deselect every selected row myself.
I solved it like this:

void RadGridViewMouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    var s = e.OriginalSource as FrameworkElement;
    var parentRow = s.ParentOfType<GridViewRow>();
    if (parentRow != null)
    {
        //When the SelectionMode of the RadGridView is extended, then the SHIFT and
        //CTRL key has to be checked. When they are not pressed the selection is cleared,
        //and only the given row is selected (this works like this for the Extended and
        //Single SelectionMode).
        if (Keyboard.IsKeyDown(Key.LeftShift) == true ||
            Keyboard.IsKeyDown(Key.RightShift) == true ||
            Keyboard.IsKeyDown(Key.LeftCtrl) == true ||
            Keyboard.IsKeyDown(Key.RightCtrl))
        {
            parentRow.IsSelected = true;                                     
        }
        else
        {
            _radGridView.SelectedItems.Clear();
            parentRow.IsSelected = true;                  
        }
    }
}

0
Dimitrina
Telerik team
answered on 15 Oct 2013, 09:19 AM
Hello,

I am glad to hear you managed to resolve the issue. 

Regards,
Didie
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Martin Ivanov
Telerik team
answered on 14 Oct 2019, 08:39 AM

Hello,

A short follow up on this topic. Setting local values on GridViewRow (or GridViewCell) properties is not recommended, unless the UI virtualization is disabled. Note that the virtualization is enabled by default and disabling it will slow down the control quite a lot. 

Instead, use data bindings for setting the cell or row properties. You can do this via the RowStyle property of RadGridView or CellStyle of the column objects. Also, you can use implicit styles instead of the style properties.

For the selection matter, use the SelectedItem or SelectedItems property of RadGridView. You can read how to select an item in code, in the Programmatic Selection help article. Here is also an example in code, based on the last code snippet in this forum:

void RadGridViewMouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    var s = e.OriginalSource as FrameworkElement;
    var parentRow = s.ParentOfType<GridViewRow>();
    if (parentRow != null)
    {       
		var dataItem = (MyRowModel)parentRow.Item;
	
        if (Keyboard.IsKeyDown(Key.LeftShift) ||
            Keyboard.IsKeyDown(Key.RightShift) ||
            Keyboard.IsKeyDown(Key.LeftCtrl) ||
            Keyboard.IsKeyDown(Key.RightCtrl))
        {
			radGridView.SelectedItems.Add(dataItem);            
        }
        else
        {
            _radGridView.SelectedItems.Clear();
			radGridView.SelectedItems.Add(dataItem);                    
        }
    }
}

Regards,
Martin Ivanov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
Roland Vonk
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Roland Vonk
Top achievements
Rank 1
Fatema
Top achievements
Rank 1
Vlad
Telerik team
Eric
Top achievements
Rank 1
Martin Ivanov
Telerik team
Share this question
or