none
Overriding DataGridView right-click context menu RRS feed

  • Question

  • Hi

    I can set a WinForms DataGridView's ContextMenu which appears upon right-clicking the grid. However, if you are inside a cell - editing the cell - and you right click, the standard Windows editing context menu pops up. To overcome this, and have my own ContextMenu appear, even when editing, I have the following code:

    Private Sub splitsDataGridView_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles splitsDataGridView.EditingControlShowing
            e.Control.ContextMenuStrip = ContextMenuStrip1
    End Sub

    This works, but doesn't seem to work when the cell can an AutoComplete set (in my example, AutoCompleteMode is set to SuggestAppend, with AutoCompleteSource set to CustomSource). Any ideas how to achieve this?


    Fabricio Rodriguez - Pretoria, South Africa

    Wednesday, July 26, 2017 2:38 PM

Answers

  • Hi

    Here is some code, (attribute in comments), which might help. The only changes I made to the code from the link was the addition of the ContextMenuStrip assignment. The ContextMenuStrip shows upon normal right click and on right click in edit mode.

    ' example taken from
    ' http://vb.net-informations.com/datagridview/vbauto.htm
    
    ' Form1 with blankDataGridView1
    
    Option Strict On
    Option Explicit On
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            DataGridView1.ColumnCount = 3
            DataGridView1.Columns(0).Name = "Product ID"
            DataGridView1.Columns(1).Name = "Product Name"
            DataGridView1.Columns(2).Name = "Product Price"
    
            Dim row As String() = New String() {"1", "Product 1", "1000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"2", "Product 2", "2000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"3", "Product 3", "3000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"4", "Product 4", "4000"}
            DataGridView1.Rows.Add(row)
        End Sub
    
        Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
            Dim titleText As String = DataGridView1.Columns(1).HeaderText
            If titleText.Equals("Product Name") Then
    
                ' added this line
                e.Control.ContextMenuStrip = ContextMenuStrip1
    
                Dim autoText As TextBox = TryCast(e.Control, TextBox)
                If autoText IsNot Nothing Then
                    autoText.AutoCompleteMode = AutoCompleteMode.Suggest
                    autoText.AutoCompleteSource = AutoCompleteSource.CustomSource
                    Dim DataCollection As New AutoCompleteStringCollection()
                    addItems(DataCollection)
                    autoText.AutoCompleteCustomSource = DataCollection
                End If
            End If
        End Sub
    
        Public Sub addItems(ByVal col As AutoCompleteStringCollection)
            col.Add("Product 1")
            col.Add("Product 2")
            col.Add("Product 3")
            col.Add("Product 4")
            col.Add("Product 5")
            col.Add("Product 6")
        End Sub
    
    End Class


    Regards Les, Livingston, Scotland

    Wednesday, July 26, 2017 3:56 PM

All replies

  • Hi

    Here is some code, (attribute in comments), which might help. The only changes I made to the code from the link was the addition of the ContextMenuStrip assignment. The ContextMenuStrip shows upon normal right click and on right click in edit mode.

    ' example taken from
    ' http://vb.net-informations.com/datagridview/vbauto.htm
    
    ' Form1 with blankDataGridView1
    
    Option Strict On
    Option Explicit On
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            DataGridView1.ColumnCount = 3
            DataGridView1.Columns(0).Name = "Product ID"
            DataGridView1.Columns(1).Name = "Product Name"
            DataGridView1.Columns(2).Name = "Product Price"
    
            Dim row As String() = New String() {"1", "Product 1", "1000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"2", "Product 2", "2000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"3", "Product 3", "3000"}
            DataGridView1.Rows.Add(row)
            row = New String() {"4", "Product 4", "4000"}
            DataGridView1.Rows.Add(row)
        End Sub
    
        Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
            Dim titleText As String = DataGridView1.Columns(1).HeaderText
            If titleText.Equals("Product Name") Then
    
                ' added this line
                e.Control.ContextMenuStrip = ContextMenuStrip1
    
                Dim autoText As TextBox = TryCast(e.Control, TextBox)
                If autoText IsNot Nothing Then
                    autoText.AutoCompleteMode = AutoCompleteMode.Suggest
                    autoText.AutoCompleteSource = AutoCompleteSource.CustomSource
                    Dim DataCollection As New AutoCompleteStringCollection()
                    addItems(DataCollection)
                    autoText.AutoCompleteCustomSource = DataCollection
                End If
            End If
        End Sub
    
        Public Sub addItems(ByVal col As AutoCompleteStringCollection)
            col.Add("Product 1")
            col.Add("Product 2")
            col.Add("Product 3")
            col.Add("Product 4")
            col.Add("Product 5")
            col.Add("Product 6")
        End Sub
    
    End Class


    Regards Les, Livingston, Scotland

    Wednesday, July 26, 2017 3:56 PM
  • To overcome this, and have my own ContextMenu appear, even when editing, I have the following code:

    Private Sub splitsDataGridView_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles splitsDataGridView.EditingControlShowing
            e.Control.ContextMenuStrip = ContextMenuStrip1
    End Sub

    This works, but doesn't seem to work when the cell can an AutoComplete set (in my example, AutoCompleteMode is set to SuggestAppend, with AutoCompleteSource set to CustomSource). Any ideas how to achieve this?


    Fabricio Rodriguez - Pretoria, South Africa

    Hi Fabs1977,

    About your description, you mean that the default context menu will appear when you use DataGridview AutoComplete Textbox and right click the cell?

    If yes, I try to do this and it works fine, it just appear the ContextMenuStrip1.

     Dim titleText As String = DataGridView1.Columns(1).HeaderText
            If titleText.Equals("Name") Then
                e.Control.ContextMenuStrip = ContextMenuStrip1
    
                Dim autoText As TextBox = TryCast(e.Control, TextBox)
                If autoText IsNot Nothing Then
    
                    autoText.AutoCompleteMode = AutoCompleteMode.SuggestAppend
                    autoText.AutoCompleteCustomSource.AddRange(New String() {"Name1", "Name1", "Name1", "Name1", "Name1"})
                    autoText.AutoCompleteSource = AutoCompleteSource.CustomSource
    
                               End If
            End If

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 27, 2017 6:33 AM
    Moderator
  • Thanks Cherry. I did try that, however, if you're editing a cell (with AutoComplete turned on) and you right click on it, it still brings up the stock Windows context menu (the same one that you get if you right-click in Notepad), instead of bringing up my own ContextMenu.

    Fabricio Rodriguez - Pretoria, South Africa

    Friday, July 28, 2017 6:11 AM
  • Hi Fabs1997,

    I also test my code and do one dynamic picture, please help me to confirm if I understand your mean correct.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 28, 2017 6:37 AM
    Moderator
  • That's so weird... When I do it - if I right-click on the cell with the AutoComplete, the EditingControlShowing event doesn't even trigger! But it triggers in the other cells. It does actually trigger if I am NOT in the autocomplete cell, and then I right-click on the AutoComplete cell. But if I'm already in the AutoComplete cell, and then I right-click in it, the EditingControlShowing doesn't fire, and the standard Windows context menu pops up.


    Fabricio Rodriguez - Pretoria, South Africa

    Friday, July 28, 2017 7:55 AM
  • Hi Fabs1977,

    Can you provide simple code that can provide your issue here? It is helpful to me to analyze this issue.

    Thanks for your understanding.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 28, 2017 8:00 AM
    Moderator
  • That's so weird... When I do it - if I right-click on the cell with the AutoComplete, the EditingControlShowing event doesn't even trigger! But it triggers in the other cells. It does actually trigger if I am NOT in the autocomplete cell, and then I right-click on the AutoComplete cell. But if I'm already in the AutoComplete cell, and then I right-click in it, the EditingControlShowing doesn't fire, and the standard Windows context menu pops up.


    Fabricio Rodriguez - Pretoria, South Africa

    Hi

    The code I posted works OK for me in all cases. The one thing I didn't mention was to make sure you have set the DataGridView - ContextMenuStrip Property to the ContextMenuStrip used.

    But, since you are getting your ContextMenuStrip OK then I imagine you have already set that.


    Regards Les, Livingston, Scotland

    Friday, July 28, 2017 10:54 AM