DataGridViewColumn.ContextMenuStrip Property


Gets or sets the shortcut menu for the column.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

Public Overrides Property ContextMenuStrip As ContextMenuStrip

Property Value

Type: System.Windows.Forms.ContextMenuStrip

The ContextMenuStrip associated with the current DataGridViewColumn. The default is null.

The shortcut menu appears when a user clicks the right mouse button in the column's display area.

The following code example uses the ContextMenuStrip property to add functionality for changing a cell's background color.

WithEvents toolStripItem1 As New ToolStripMenuItem()

Private Sub AddContextMenu()
    toolStripItem1.Text = "Redden"
    Dim strip As New ContextMenuStrip()
    For Each column As DataGridViewColumn _
        In dataGridView.Columns()

        column.ContextMenuStrip = strip
End Sub
' Change the cell's color.
Private Sub toolStripItem1_Click(ByVal sender As Object, _
    ByVal args As EventArgs) _
    Handles toolStripItem1.Click

    dataGridView.Rows(mouseLocation.RowIndex) _
        .Cells(mouseLocation.ColumnIndex) _
        .Style.BackColor = Color.Red
End Sub

Private mouseLocation As DataGridViewCellEventArgs

' Deal with hovering over a cell.
Private Sub dataGridView_CellMouseEnter(ByVal sender As Object, _
    ByVal location As DataGridViewCellEventArgs) _
    Handles DataGridView.CellMouseEnter

    mouseLocation = location
End Sub

.NET Framework
Available since 2.0
Return to top