Datagridview: contexmenustrip  vs. ColumnHeaderMouseClick right click

Posted on 2007-07-26
Last Modified: 2008-01-09
Hello. I am working with a datagridview.  I have a contextmenustrip on my form.  When I right click on a column header in the dgv, I want to do something.  The problem is, when I right click the header, the contexmenustrip pops up and the ColumnHeaderMouseClick never gets fired.  Is there a way around this?  
Question by:adwooley2
    LVL 46

    Accepted Solution

    Hello adwooley2,

    Set the contextmenustrip property to nothing, then show the context menu via code....

        Private Sub DataGridView1_ColumnHeaderMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.ColumnHeaderMouseClick
            If e.Button = Windows.Forms.MouseButtons.Right Then
                ContextMenuStrip1.Show(DataGridView1, New Point(e.X, e.Y))
            End If
        End Sub



    Author Comment

    That's really close, Wayne, I can feel it.
    Here's what I want to do:

    Header left clicked: Sort the dgv
    Header right clicked: Run the freeze code
    Right click anywhere else but header: show the contextmenustrip

    Any way to do that? Sorry - I just can't seem to get it straight.

    Author Comment

    Wayne - appreciate you steering me the right way.  What I ended up doing was this:

    Set the contextmenustrip property to nothing for datagridview
    Loop through the rows and set the ContextMenuStrip = Me.ContextMenuStrip1.  This way, it doesn't set the columnheaders to have a contextmenustrip.


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video discusses moving either the default database or any database to a new volume.

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now