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

Cell/Row selection when mouse right click

15 Answers 1591 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Marco
Top achievements
Rank 1
Marco asked on 08 Apr 2010, 05:33 PM
Hello,
I'm using Q1 2010 RadGridView in an application where I have a context menu that displays options based on the cell value.

It works perfectly when the user clicks on the cell (CurrentCell property is being loaded) and then right click on it to get the context menu.  However, I'd like to provide a behavior like excel in which, when the user rightclicks on any cell, it gets selected and the corresponding context menu will consider the actual cell value.
I couldn't find any property like "SelectCellOnMouseRightClick" or workaround that could help me achieve this.  Is there a way I can have this behavior in my RadGridView?

My SelectionMode="Extended", as there are other operation that require multiple row selection.

Thanks in advance

15 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 09 Apr 2010, 06:21 AM
Hi Marco,

You can use the approach demonstrated in this demo. Check the code in RadContextMenu_Opened for more info.

All the best,
Vlad
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Chris Boarman
Top achievements
Rank 1
answered on 16 Jun 2010, 07:34 AM
I have a similar requirement for a context menu to appear when user right-clicks a row. I tried the recommended example (handling the RadContextMenu_Opened event and setting IsCurrent and IsSelected to true) and it worked, except for one small problem - if the grid is in multiselect mode, any previous rows selected remain selected, even if the shift or control key is not pressed. I would expect the default behavior (when previous selctions exist) should be as follows:

1. A right-click select should cause all other selected items to be unselected, then show the context menu. 
2. A shift / right-click should extend the selection, then show the context menu.
3. A control right-click should add the row to the selection, then show context menu.

Is there some way I could modify the recommended example to achieve this behavior?

For reference, here is the code I am using:
        Private Sub RadContextMenu_Opened(ByVal sender As System.ObjectByVal e As System.Windows.RoutedEventArgs) 
            Dim menu As RadContextMenu = DirectCast(sender, RadContextMenu) 
            Dim row As GridViewRow = menu.GetClickedElement(Of GridViewRow)() 
 
            If row IsNot Nothing Then 
                row.IsCurrent = True 
                row.IsSelected = True 
                ' The following is not neeed 'cause we are in row select mode (left it in in case we ever  
                ' need it <ccb 6.10.10> 
                'Dim cell As GridViewCell = menu.GetClickedElement(Of GridViewCell)() 
                'If cell IsNot Nothing Then 
                '    cell.IsCurrent = True 
                'End If 
                menu.IsOpen = True ' needed to show the context menu 
            Else 
                menu.IsOpen = False 
            End If 
 
        End Sub 
    End Class 



0
Milan
Telerik team
answered on 16 Jun 2010, 11:57 AM
Hello Chris Boarman,

Currently you can force the grid to unselect all rows by clearing the SelectedItems collection (SelectedItems.Clear()). After the selection is cleared you can select the desired row by setting its IsSelected property to true or simply adding its associated data item to the SelectedItems collection.

We will consider your request regarding right-click selection.

Thank you for your feedback.


Best wishes,
Milan
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Leon
Top achievements
Rank 1
answered on 24 Jun 2010, 07:40 PM
When I click on the Demo...

it says that "Could not load file or assembly "GridView" or one of its dependencies...

Please help..
0
Milan
Telerik team
answered on 25 Jun 2010, 09:32 AM
Hello Leon,

Could you please clear your XBAP cache by executing the following command from your Visual Studio command prompt:

mage -cc


Sincerely yours,
Milan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Leon
Top achievements
Rank 1
answered on 25 Jun 2010, 01:39 PM
Still no luck...

same issue...can someone just paste the code example here.

Thanks

0
Joel Palmer
Top achievements
Rank 2
answered on 12 Apr 2012, 10:17 PM
Can I just say that it was frustrating knowing that there is an example somewhere but the referenced hyperlink didn't go anywhere.  Here is how I got it working.

XAML:
<telerikGrid:RadGridView
            x:Name="controlGrid"
            Grid.Row="0"
            ShowGroupPanel="False"
            AutoGenerateColumns="True"     
            IsFilteringAllowed="False"
            CanUserDeleteRows="False"
            CanUserReorderColumns="False"
            CanUserSortColumns="False"
            SelectionUnit="Cell"
            SelectionMode="Single" 
            CanUserSelect="True"
            CellEditEnded="cellEditEnded"
            RowLoaded="gridRowLoaded"
            SelectedCellsChanged="gridSelectedCellsChanged"
    >
 
    <telerik:RadContextMenu.ContextMenu>
        <telerik:RadContextMenu
            x:Name="GridContextMenu"
            Opened="GridContextMenu_Opened">
 
            <MenuItem
                Header="Add Row"
                Click="mnuAddRow_Click"
                IsEnabled="{Binding ElementName=controlGrid,
                Path=IsReadOnly, Converter={StaticResource InvertedBooleanConverter1}}" />
 
            <MenuItem
                Header="Add Column"
                Click="mnuAddColumn_Click"
                IsEnabled="{Binding ElementName=controlGrid,
                Path=IsReadOnly, Converter={StaticResource InvertedBooleanConverter1}}" />
             
            <MenuItem Header="-" />
             
            <MenuItem Header="P_roperties..."
                Click="mnuCellProperties_Click"
                IsEnabled="{Binding ElementName=controlGrid,
                Path=IsReadOnly,
                Converter={StaticResource InvertedBooleanConverter1}}" />
        </telerik:RadContextMenu>
         
    </telerik:RadContextMenu.ContextMenu>
 
</telerikGrid:RadGridView>


C#
private void GridContextMenu_Opened(object sender, RoutedEventArgs e)
{
    try
    {
        RadContextMenu menu = (RadContextMenu)sender;
        GridViewCell cell = menu.GetClickedElement<GridViewCell>();
        if (cell != null)
        {
            cell.IsSelected = true;
            cell.IsCurrent = true;
        }
    }
    catch (Exception ex)
    {
        exceptionHandler(ex);
    }
}

0
Maya
Telerik team
answered on 13 Apr 2012, 09:01 AM
Hi Joel,

I am sorry to see that you have been frustrated with the information provided. The link targets WPF version of the examples that (when being posted) used to be in XBAP (i.e. they could be opened directly in IE). You can find the same example for WPF in your local installation or in our Silverlight demos as well (please follow this link(http://demos.telerik.com/wpf/?GridView/RowContextMenu) for a reference).
Furthermore, you can find information for similar scenarios in our forums as well (link1, link2, link3, link4).

Greetings,
Maya
the Telerik team

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

0
May
Top achievements
Rank 1
answered on 09 May 2012, 01:56 PM
Anyway to enable cell/row selection on mouse right click outside the "code behind file"? I need this to work on all my grids and would prefere if I could handel it in the style or/and viewmodel? Anyone?
0
Maya
Telerik team
answered on 09 May 2012, 02:20 PM
Hi May,

You can create custom behavior and attach it to each grid you require such functionality for. 

Kind regards,
Maya
the Telerik team

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

0
Andrew
Top achievements
Rank 1
answered on 24 Jan 2013, 04:39 PM
I've implemented cell selection on right-click, but it's not removing the orange background on the previously highlighted cell. So each time I right-click I end up with more and more orange cells. I'm clearing down the SelectedItems, but it makes no difference. Here is my grid XAML:-

<telerik:RadGridView Name="grdMain" AutoGenerateColumns="True" IsReadOnly="True" ItemsSource="{Binding Data}" SelectionMode="Single" SelectionUnit="Cell" ShowColumnHeaders="False" ShowInsertRow="False" ShowGroupPanel="False" MouseRightButtonUp="grdMain_MouseRightButtonUp" />

And here is the code-behind:-

private void grdMain_MouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    var s = e.OriginalSource as FrameworkElement;
    if (s is TextBlock)
    {
        var cell = s.ParentOfType<GridViewCell>();
        grdMain.SelectedItems.Clear();
        cell.IsCurrent = true;
        cell.IsSelected = true;
    }
}

Any idea why this is happening?

0
Maya
Telerik team
answered on 24 Jan 2013, 04:49 PM
Hello Andrew,

Could you try clearing the SelectedCells collection instead ? Do you get the same behavior ?  

Regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Andrew
Top achievements
Rank 1
answered on 25 Jan 2013, 08:10 AM
Hi, clearing down SelectedItems and SelectedCells didn't work (both were always empty). In the end I used the following:-

                if (grdMain.CurrentCell != null)
                {
                    grdMain.CurrentCell.IsCurrent = false;
                    grdMain.CurrentCell.IsSelected = false;
                }

Hope this helps someone.
0
Js
Top achievements
Rank 1
answered on 11 Jun 2013, 11:25 AM
Add the GridCellInfo to the SelectedCells collection and the SelectedCells.Clear will work correctly:

grdMain.SelectedCells.Clear();
cell.IsCurrent = true;
cell.IsSelected = true;
   grdMain.SelectedCells.Add(new GridViewCellInfo(cell));


Regards,
J.S.
0
Nimisha
Top achievements
Rank 1
answered on 09 Feb 2015, 07:44 AM
Hi Joel,

Thanks a lot. That was a useful piece of information, with example. :)
Tags
GridView
Asked by
Marco
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Chris Boarman
Top achievements
Rank 1
Milan
Telerik team
Leon
Top achievements
Rank 1
Joel Palmer
Top achievements
Rank 2
Maya
Telerik team
May
Top achievements
Rank 1
Andrew
Top achievements
Rank 1
Js
Top achievements
Rank 1
Nimisha
Top achievements
Rank 1
Share this question
or