[RESOLVED] DataGridView.ContextMenuStrip Property Blocks CellMouseClick Event?
If you assign a ContextMenuStrip to the DataGridView.ContextMenuStrip property and then right-click a cell, the CellMouseClick event doesn't fire. Why would the ContextMenuStrip block this event from occurring? What's the reasoning?
My workaround at the moment is to separate the ContextMenuStrip from the DataGridView and show it with code, but this seems unnecessary.
Re: DataGridView.ContextMenuStrip Property Blocks CellMouseClick Event?
Quote:
Originally Posted by
ForumAccount
If you assign a ContextMenuStrip to the DataGridView.ContextMenuStrip property and then right-click a cell, the CellMouseClick event doesn't fire. Why would the ContextMenuStrip block this event from occurring? What's the reasoning?
My workaround at the moment is to separate the ContextMenuStrip from the DataGridView and show it with code, but this seems unnecessary.
Have you used mousedown event. i still often use contextmenu in datagridview.
or you can use:
Code:
Private Sub Userdatagridview_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Userdatagridview.Click
If e.Button = Windows.Forms.MouseButtons.Right Then
ContextMenuStrip1.Show(Windows.Forms.Cursor.Position.X, Windows.Forms.Cursor.Position.Y)
End If
End Sub
Re: DataGridView.ContextMenuStrip Property Blocks CellMouseClick Event?
The workaround is fine, but what you really need to understand is that you are setting a context menu for the actual datagridview, and you can also set context menu objects for rows, or even individual cells..
These are valid
Code:
DataGridView1.Rows(x).ContextMenuStrip = SomeMenuStrip
DataGridView1.Rows(x).Cells(x).ContextMenuStrip = SomeMenuStrip
However that doesn't really help with your specific situation of wanting CellMouseClick to fire. So it really depends on what you want to do when the user right clicks on a cell. There are events you can use.
For example, this code would highlight whatever row i right click on (deselecting any other row) and show a contextmenu. This is without ever actually assigning the context menu to the datagridview at design time.
Code:
Private Sub DataGridView1_CellContextMenuStripNeeded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellContextMenuStripNeededEventArgs) Handles DataGridView1.CellContextMenuStripNeeded
DataGridView1.ClearSelection()
DataGridView1.Rows(e.RowIndex).Selected = True
e.ContextMenuStrip = ContextMenuStrip1
End Sub
Ocourse this doesn't fire the CellMouseClick, but it is an indication to you as the developer that this is happening, so you can perform whatever code you would want to run in this situation into this event, or for easier maintenance, a common routine you can call from both events.
Re: [RESOLVED] DataGridView.ContextMenuStrip Property Blocks CellMouseClick Event?
I reactivate this thread because the ContextMenuStrip does not work if we are you are in edit mode.
In a cell in edit mode if you do a right click with the mouse, you don't go on ContextMenuStrip, but you will see a standard menu with cut/paste/copy.
It is OK if you do a right click in other cell than the cell in edit mode.
Thanks for a solution.
Dominique
Re: [RESOLVED] DataGridView.ContextMenuStrip Property Blocks CellMouseClick Event?
I found a solution.
Code:
Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, _
ByVal e As DataGridViewEditingControlShowingEventArgs) _
Handles DataGridView1.EditingControlShowing
If TypeOf e.Control Is TextBox Then
With DirectCast(e.Control, TextBox)
.ContextMenuStrip = ContextMenuStrip2
End With
End If
End Sub
I found that at :
http://social.msdn.microsoft.com/for...-c2206739c8fe/
and
http://stackoverflow.com/questions/2...-in-a-datagrid
In the second link, the guy asked how to merge the two ContextMenuStrip (our specific and the standard).
It is a good question. If any people have an answer, it will be good.