Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGridView Right Click Select

Status
Not open for further replies.

schwarem

Programmer
Apr 18, 2002
159
US
How do I get the datagridview to select the row the mouse is one when you right click on the row. I want to open a context menu based on the row clicked on.
 
Assuming you have a DataGridView dgv1 and an accompanying BindingSource bs1 then

Code:
	Private Sub SelectDGVRow(ByVal Row As Integer)

		bs1.Position = Row
		dgv1.Rows(Row).Selected = True

	End Sub

	Private Sub dgv1_RowHeaderMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgv1.RowHeaderMouseClick

		If e.Button = Windows.Forms.MouseButtons.Right Then
			SelectDGVRow(e.RowIndex)
		End If

	End Sub

	Private Sub dgv1_CellMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgv1.CellMouseClick

		If e.Button = Windows.Forms.MouseButtons.Right Then
			SelectDGVRow(e.RowIndex)
		End If

	End Sub

This allows for Right-Clicking either a RowHeader or a Cell

Hope this helps.

[vampire][bat]
 
The cellMouseClick event only fires when you left click on the mouse. It doesn't fire when you right click.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top