Select row on WinForm grid with right-click

9 posts, 0 answers
  1. Tim Black
    Tim Black avatar
    9 posts
    Member since:
    Nov 2002

    Posted 07 May 2012 Link to this post

    I've searched this forum and have not found a post specific to this issue.  The MouseClick event gives me the e.button property so I know if right button used but no row index.  The CellClick event gives me the Row.Index but not which button clicked. 

    Any assistance would be greatly appreciated.  Thanks
  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 08 May 2012 Link to this post

    Hi Tim, 

    You can get this just from the SelectedRows property of the RadGridView
    For example
    this.radGridView1.SelectionChanged += new EventHandler(radGridView1_SelectionChanged);

    void radGridView1_SelectionChanged(object sender, EventArgs e)
    {
        if (this.radGridView1.SelectedRows.Count > 0)
        {
          this.Text = this.radGridView1.SelectedRows[0].Index.ToString();         
        }
     
    }

    If you are using single selection mode, then this will show you the currently selected index of the row which will fire when right clicking as well as left clicking. 

    Hope that helps
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 10 May 2012 Link to this post

    Hello Tim,

    Could you please share with us, what exactly is your scenario, so we can point you to the right direction.
     
    Kind regards,
    Stefan
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
  5. MikeB
    MikeB avatar
    42 posts
    Member since:
    May 2008

    Posted 23 Aug 2012 Link to this post

    Version 2012.1.321.40 Winform Controls

    RadGridView

    I have a custom context menu that needs to be displayed only when the user right clicks on a GridDataRow.  I need to know what row the cursor was on when the right mouse button is clicked.  The Cell.Click event args does not include what button was clicked.  The Grid.MouseClick does include the button but the context menu should not be displayed unless a data rows is clicked.

    Using the Grid.MouseClick, I haven't found a way to determine what (if any) data row is clicked using the grid mouse click and I can't use the current row.  Basically, if a data row is not clicked (some other part of the grid - header row,etc is clicked) I do not want to display the context menu.

    Using the Cell.Click, I cannot determine what button was clicked.  And the Context Menu should not be displayed if the left or center mouse button is clicked.

    Is there a way that I can do this?

    The Grid.GetChildAtPoint looked promising but it is always null.

    This is an application that has been in use for a while and the business users are accustomed to it working this way.  If I have to change the behavior... well you know how that is.

    Thanks
    Mike 
  6. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 28 Aug 2012 Link to this post

    Hello Mike,

    Thank you for writing.

    The correct way to work with the context menu of the grid is to use the ContextMenuOpening event. It is fired when the user right click the grid. In the event handler you can check whether the ContextMenuProvider is GridDataCellElement and of so, a data cell is clicked and modify the context menu (which is available in the ContextMenu property from the event args). Alternatively you can cancel the context menu. Here is an example:
    void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
    {
        GridDataCellElement dataCellElement = e.ContextMenuProvider as GridDataCellElement;
        if (dataCellElement != null)
        {
            //data cell is clicked
     
            //here is how to get the clicked row
            GridViewRowInfo clickedRow = dataCellElement.RowInfo;
     
     
        }
        else
        {
            //clicked on non data cell, cancel the context menu
            e.Cancel = true;
        }
    }

    More information about the context menu in the grid can be found in this help section: http://www.telerik.com/help/winforms/gridview-context-menus-modifying-the-default-context-menu.html.

    Let us know if you need anything else.

    Kind regards,
    Stefan
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
  7. MikeB
    MikeB avatar
    42 posts
    Member since:
    May 2008

    Posted 28 Aug 2012 Link to this post

    Stefan,

    Yes, I realized that I was going about it the wrong way and did end up using the ContextMenuOpenening.  I apologize, I should have come back and deleted or edited the post.

    Thanks
    Mike 
  8. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 03 Sep 2012 Link to this post

    That's OK Mike. Let us know if you have any other questions.

    Kind regards,
    Stefan
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
  9. Barış
    Barış avatar
    1 posts
    Member since:
    Aug 2013

    Posted 24 Aug 2013 Link to this post

    Hi, i have the same issue. But openingcontext event dont work for me. But when i right click to a column header cell, my context menu  stil shows. And I dont want it. Is there a way to use "old" mouse_click event and datagridview.hittest like method? Because I want to know excatly where did i click button (x,y) on the grid

    Best Regards
    Barış
  10. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 26 Aug 2013 Link to this post

    Hi Barış,

    As I previously suggested, the easiest way to handle the context menus in RadGridView is the ContextMenuOpening event. To prevent showing the context menu of the header cells, please consider the following code:
    void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
    {
        GridHeaderCellElement headerCell = e.ContextMenuProvider as GridHeaderCellElement;
        if (headerCell != null)
        {
            e.Cancel = true; //header cell is clicked, prevent showing the context menu
        }
    }

    I hope this helps.
     

    Regards,
    Stefan
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
    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 >>
Back to Top
UI for WinForms is Visual Studio 2017 Ready