Results 1 to 5 of 5

Thread: [RESOLVED] DataGridView.ContextMenuStrip Property Blocks CellMouseClick Event?

  1. #1

    Thread Starter
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Resolved [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.

  2. #2
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    Re: DataGridView.ContextMenuStrip Property Blocks CellMouseClick Event?

    Quote Originally Posted by ForumAccount View Post
    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
    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  4. #4
    Junior Member
    Join Date
    Aug 2007
    Posts
    21

    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

  5. #5
    Junior Member
    Join Date
    Aug 2007
    Posts
    21

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width