Add Context Menu to DataGridView Column Header in vb.net

Good Morning! Last night I spent a good portion of time analyzing my Automatic Tag Generator Plugin for Windows Live Writer and trying to get the tags it generates to appear in the keyword section in Windows Live Writer. The problem is that if you inherit the Notificationhook you can’t seem to be able to access the Contentsource and vice versa is true as well. So if you have any ideas on this I would be grateful if you would share them.

So today’s topic is realtively straight forward but somebody asked yesterday so here it is. We want to add a context menu to a DataGridView Column Header. Make it a great day!

Private Sub FLV_Grid_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles FLV_Grid.CellMouseDown
‘Note:FLV_Grid is bound to DataGridView object on form
Dim HitTest As System.Windows.Forms.DataGridView.HitTestInfo
        Dim CM As New ContextMenuStrip
        HitTest = DataGridView1.HitTest(e.X, e.Y)
        Select Case e.Button
            Case System.Windows.Forms.MouseButtons.Right
                Select Case e.Button
                    Case MouseButtons.Right
                        CM.Items.Clear()
                        CM.Items.Add("Item one")
                        CM.Items.Add("Item two")
                        CM.Items.Add("Item three")
                        CM.Items.Add("Item four")
                        DataGridView1.ContextMenuStrip = CM
                        DataGridView1.ContextMenuStrip.Show(DataGridView1, New Point(e.X, e.Y))
                End Select
        End Select

End Sub

Technorati Tags: ,,,,,,,,,
,,,,,,,,
,,,,,,
,,,,,,,,

Advertisements
  1. #1 by Unknown on May 16, 2010 - 10:28 am

    struggled for a long time with it … here is the solution:http://blog.bitlinkit.com/post/Right-Click-and-Auto-Row-Select-with-a-ContextMenuStrip.aspx

  2. #2 by McLovin on February 26, 2013 - 4:30 am

    Nice, thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: