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?

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.
Who is Participating?
CodeCruiserConnect With a Mentor Commented:
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

SheldonCAuthor Commented:
Any ideas guys?
jQuery can help you in this scenario.

check this out:

Other mouse events:

you can also prevent contextmenu poping up.
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

SheldonCAuthor Commented:
Isn't jquery a javascript command? I am using VB .Net
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.
SheldonCAuthor Commented:
can I get a code sample?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.