I'm trying to give a context menu to my user when they right click on a particular row in my datagrid. I created two functions that pass back the indicies for the row and column. The following code is located in the Mouse_Clicked event point for the datagrid itself. When I right click the first time everything works perfetly, but the next time I right click the context menu comes up but nothing else gets updated. If I left click on the datagrid then right click it behaves normally, so I guess my question is why doesn't the mouse_click event fire correctly when the user right clicks? Here is the code:
Code:
Private Sub dgridContractors_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgridContractors.MouseClick
If e.Button = Windows.Forms.MouseButtons.Right Then
'Hightlight the row
Me.dgridContractors.Rows(dgridContractors.CurrentRow.Index).Selected = False
Me.dgridContractors.Rows(NewRowIndex(e.X, e.Y)).Selected = True
With Me.ContextMenuStrip1
.Items.Clear()
.Items.Add("Delete this record")
.Items.Add("Sort by Column """ & dgridContractors.Columns(NewColumnIndex(e.X, e.Y)).HeaderText & """")
End With
ContextMenuStrip1.Show(Windows.Forms.Cursor.Position)
End If
End Sub
Private Function NewRowIndex(ByVal x As Integer, ByVal y As Integer) As Integer
Dim info As DataGridView.HitTestInfo = dgridContractors.HitTest(x, y)
Return info.RowIndex
info = Nothing
End Function
Private Function NewColumnIndex(ByVal x As Integer, ByVal y As Integer) As Integer
Dim info As DataGridView.HitTestInfo = dgridContractors.HitTest(x, y)
Return info.ColumnIndex
info = Nothing
End Function