How do I change focus of the row a datagridview control to the row that is right-clicked before a contextmenustrip option is selected or shown?

Posted on 2011-10-17
Last Modified: 2012-08-13
I have a context menu strip attached to a datagridview control. My context menu strip has two options, add a row or remove a row. I am able to do both successfully but I am having a problem remove the correct row when I right click on the row in question. It would only remove the row that was last selected or  in other words, that I last left clicked on.

I attempted to use the mouse_click event of the datagridview control but it is executed after the menu strip options have been displayed and selected.
Question by:SheldonC

    Author Comment

    Any ideas guys?
    LVL 22

    Expert Comment

    jQuery can help you in this scenario.

    check this out:

    Other mouse events:

    you can also prevent contextmenu poping up.

    Author Comment

    Isn't jquery a javascript command? I am using VB .Net
    LVL 83

    Expert Comment

    Little tip: datagridview is winforms. Gridview is webforms :-)

    Either handle the CellClick event of the grid and set the currentcell or handle the Click event, supply the x,y to HitTest method to find the cell under click and set the currentcell.

    Author Comment

    can I get a code sample?
    LVL 83

    Accepted Solution

    Either try this

    Private Sub dataGridView_CellMouseDown(sender As Object, e As DataGridViewCellMouseEventArgs)
    	If e.Button = MouseButtons.Right Then
    		dataGridView.CurrentCell = dataGridView(e.ColumnIndex, e.RowIndex)
    	End If
    End Sub

    Open in new window

    Or this

    Private Sub SubClassedGridView_MouseDown(sender As Object, e As MouseEventArgs)
    	' Sets is so the right-mousedown will select a cell
    	Dim hti As DataGridView.HitTestInfo = Me.HitTest(e.X, e.Y)
    	' Clear all the previously selected rows
    	' Set as selected
    	Me.Rows(hti.RowIndex).Selected = True
    End Sub

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Suggested Solutions

    I'm currently working for a company where I have to upgrade over 50 VB6 programs to VB.NET 2008.  So far I'm about half way through, and I've learned quite a few tricks that drastically improve the performance of VB.NET apps. Because there are a…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now