DataGridViewRow.ContextMenuStrip Property


Gets or sets the shortcut menu for the row.

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 DataGridViewRow. The default is null.

Exception Condition

When getting the value of this property, the row is in a DataGridView control and is a shared row.

This property indicates the shortcut menu that is displayed when the user right-clicks the row unless the DataGridView control CellContextMenuStripNeeded event or the row ContextMenuStrip property overrides the shortcut menu for the specific cell that was clicked.

When the DataGridView control DataSource property is set or its VirtualMode property is true, getting the value of the ContextMenuStrip property raises the RowContextMenuStripNeeded event of the control and returns the value of the DataGridViewRowContextMenuStripNeededEventArgs.ContextMenuStrip property as specified in the event handler. If there are no handlers for the event, getting the value of the ContextMenuStrip property returns the previously specified value or its default value of null.

Handling the RowContextMenuStripNeeded event is primarily useful when working with large amounts of data to avoid performance penalties when setting the row ContextMenuStrip value for multiple rows. For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.

Changing this property raises the RowContextMenuStripChanged event on the owning DataGridView, if one exists.

If the current row is shared, use the GetContextMenuStrip method rather than getting the value of this property.

The following code example demonstrates how to use the DataGridViewColumn.ContextMenuStrip property, which is nearly identical to the ContextMenuStrip property of the DataGridViewRow class. This code example is part of a larger code example provided in the DataGridViewColumn class.

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