DataGridViewRowContextMenuStripNeededEventArgs.ContextMenuStrip Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Gets or sets the shortcut menu for the row that raised the RowContextMenuStripNeeded event.

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

Public Property ContextMenuStrip As ContextMenuStrip

This property initially contains the value of the DataGridViewRow.ContextMenuStrip property if it has been set for a row.

The following code example handles the RowContextMenuStripNeeded event to provide a ContextMenuStrip based on the title of the employee. In this example, there are two shortcut menus, one for managers and one for all other employees. This example is part of a larger example available in the DataGridViewRowContextMenuStripNeededEventArgs class overview.

Public Sub dataGridView1_RowContextMenuStripNeeded( _
    ByVal sender As Object, _
    ByVal e As DataGridViewRowContextMenuStripNeededEventArgs) _
    Handles dataGridView1.RowContextMenuStripNeeded

    Dim dataGridViewRow1 As DataGridViewRow = _

    toolStripMenuItem1.Enabled = True

    ' Show the appropriate ContextMenuStrip based on the employees title.
    If dataGridViewRow1.Cells("Title").Value.ToString() = _
        "Sales Manager" OrElse _
        dataGridViewRow1.Cells("Title").Value.ToString() = _
        "Vice President, Sales" Then

        e.ContextMenuStrip = managerMenuStrip
        e.ContextMenuStrip = employeeMenuStrip
    End If

    contextMenuRowIndex = e.RowIndex
End Sub

.NET Framework
Available since 2.0
Return to top