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.
SheldonCAsked:
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
	Me.ClearSelection()

	' Set as selected
	Me.Rows(hti.RowIndex).Selected = True
End Sub

Open in new window

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

check this out:

http://stackoverflow.com/questions/1206203/how-to-distinguish-between-left-and-right-mouse-click-with-jquery

Other mouse events:

http://api.jquery.com/category/events/mouse-events/

you can also prevent contextmenu poping up.
0
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
0
 
CodeCruiserCommented:
@mroonal
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.
0
 
SheldonCAuthor Commented:
can I get a code sample?
0
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.