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
Medium Priority
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
  • 3
  • 2

Author Comment

ID: 36988229
Any ideas guys?
LVL 22

Expert Comment

ID: 36990319
jQuery can help you in this scenario.

check this out:


Other mouse events:


you can also prevent contextmenu poping up.

Author Comment

ID: 36992641
Isn't jquery a javascript command? I am using VB .Net
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 83

Expert Comment

ID: 37002748
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

ID: 37077789
can I get a code sample?
LVL 83

Accepted Solution

CodeCruiser earned 1200 total points
ID: 37078638
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


Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

850 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