Cell/Row selection when mouse right click

16 posts, 0 answers
  1. Marco
    Marco avatar
    23 posts
    Member since:
    Apr 2009

    Posted 08 Apr 2010 Link to this post

    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
  2. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 09 Apr 2010 Link to this post

    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.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Chris Boarman
    Chris Boarman avatar
    9 posts
    Member since:
    Dec 2009

    Posted 16 Jun 2010 Link to this post

    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 



  5. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 16 Jun 2010 Link to this post

    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.
  6. Leon
    Leon avatar
    16 posts
    Member since:
    Jun 2010

    Posted 24 Jun 2010 Link to this post

    When I click on the Demo...

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

    Please help..
  7. Milan
    Admin
    Milan avatar
    1989 posts

    Posted 25 Jun 2010 Link to this post

    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
  8. Leon
    Leon avatar
    16 posts
    Member since:
    Jun 2010

    Posted 25 Jun 2010 Link to this post

    Still no luck...

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

    Thanks

  9. Joel Palmer
    Joel Palmer avatar
    164 posts
    Member since:
    May 2009

    Posted 12 Apr 2012 Link to this post

    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);
        }
    }

  10. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 13 Apr 2012 Link to this post

    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 >>

  11. May
    May avatar
    6 posts
    Member since:
    Apr 2012

    Posted 09 May 2012 Link to this post

    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?
  12. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 09 May 2012 Link to this post

    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 >>

  13. Andrew
    Andrew avatar
    8 posts
    Member since:
    Nov 2012

    Posted 24 Jan 2013 Link to this post

    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?

  14. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 24 Jan 2013 Link to this post

    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.

  15. Andrew
    Andrew avatar
    8 posts
    Member since:
    Nov 2012

    Posted 25 Jan 2013 Link to this post

    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.
  16. Js
    Js avatar
    1 posts
    Member since:
    Jun 2013

    Posted 11 Jun 2013 Link to this post

    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.
  17. Nimisha
    Nimisha avatar
    1 posts
    Member since:
    Feb 2015

    Posted 09 Feb 2015 in reply to Joel Palmer Link to this post

    Hi Joel,

    Thanks a lot. That was a useful piece of information, with example. :)
Back to Top
UI for WPF is Visual Studio 2017 Ready