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

Select row on WinForm grid with right-click

8 Answers 708 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Tim Black
Top achievements
Rank 1
Tim Black asked on 07 May 2012, 04:45 PM
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

8 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 08 May 2012, 01:58 PM
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
0
Stefan
Telerik team
answered on 10 May 2012, 03:02 PM
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 >>
0
MikeB
Top achievements
Rank 1
answered on 23 Aug 2012, 09:33 PM
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 
0
Stefan
Telerik team
answered on 28 Aug 2012, 08:12 AM
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 >>
0
MikeB
Top achievements
Rank 1
answered on 28 Aug 2012, 03:21 PM
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 
0
Stefan
Telerik team
answered on 03 Sep 2012, 06:55 AM
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 >>
0
Barış
Top achievements
Rank 1
answered on 24 Aug 2013, 11:00 PM
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ış
0
Stefan
Telerik team
answered on 26 Aug 2013, 08:35 AM
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 >>
Tags
GridView
Asked by
Tim Black
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Stefan
Telerik team
MikeB
Top achievements
Rank 1
Barış
Top achievements
Rank 1
Share this question
or