DataGridViewRowContextMenuStripNeededEventArgs.ContextMenuStrip DataGridViewRowContextMenuStripNeededEventArgs.ContextMenuStrip DataGridViewRowContextMenuStripNeededEventArgs.ContextMenuStrip DataGridViewRowContextMenuStripNeededEventArgs.ContextMenuStrip Property

定义

获取或设置引发 RowContextMenuStripNeeded 事件的行的快捷菜单。Gets or sets the shortcut menu for the row that raised the RowContextMenuStripNeeded event.

public:
 property System::Windows::Forms::ContextMenuStrip ^ ContextMenuStrip { System::Windows::Forms::ContextMenuStrip ^ get(); void set(System::Windows::Forms::ContextMenuStrip ^ value); };
public System.Windows.Forms.ContextMenuStrip ContextMenuStrip { get; set; }
member this.ContextMenuStrip : System.Windows.Forms.ContextMenuStrip with get, set
Public Property ContextMenuStrip As ContextMenuStrip

属性值

示例

下面的代码示例将处理RowContextMenuStripNeeded事件, 以便ContextMenuStrip基于员工的标题提供。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. 此示例是DataGridViewRowContextMenuStripNeededEventArgs类概述中提供的一个更大示例的一部分。This example is part of a larger example available in the DataGridViewRowContextMenuStripNeededEventArgs class overview.

void dataGridView1_RowContextMenuStripNeeded(object sender,
    DataGridViewRowContextMenuStripNeededEventArgs e)
{
    DataGridViewRow dataGridViewRow1 = dataGridView1.Rows[e.RowIndex];

    toolStripMenuItem1.Enabled = true;

    // Show the appropriate ContextMenuStrip based on the employees title.
    if ((dataGridViewRow1.Cells["Title"].Value.ToString() ==
        "Sales Manager") ||
        (dataGridViewRow1.Cells["Title"].Value.ToString() ==
        "Vice President, Sales"))
    {
        e.ContextMenuStrip = managerMenuStrip;
    }
    else
    {
        e.ContextMenuStrip = employeeMenuStrip;
    }

    contextMenuRowIndex = e.RowIndex;
}
Public Sub dataGridView1_RowContextMenuStripNeeded( _
    ByVal sender As Object, _
    ByVal e As DataGridViewRowContextMenuStripNeededEventArgs) _
    Handles dataGridView1.RowContextMenuStripNeeded

    Dim dataGridViewRow1 As DataGridViewRow = _
    dataGridView1.Rows(e.RowIndex)

    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
    Else
        e.ContextMenuStrip = employeeMenuStrip
    End If

    contextMenuRowIndex = e.RowIndex
End Sub

注解

如果已为某行设置此属性DataGridViewRow.ContextMenuStrip , 则该属性最初包含该属性的值。This property initially contains the value of the DataGridViewRow.ContextMenuStrip property if it has been set for a row.

适用于

另请参阅