DataGridViewRow.ContextMenuStrip DataGridViewRow.ContextMenuStrip DataGridViewRow.ContextMenuStrip DataGridViewRow.ContextMenuStrip Property

定义

获取或设置行的快捷菜单。Gets or sets the shortcut menu for the row.

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

属性值

与当前 ContextMenuStrip 关联的 DataGridViewRowThe ContextMenuStrip associated with the current DataGridViewRow. 默认值为 nullThe default is null.

异常

在获取此属性的值时,行位于 DataGridView 控件中,并且是共享行。When getting the value of this property, the row is in a DataGridView control and is a shared row.

示例

下面的代码示例演示如何使用DataGridViewColumn.ContextMenuStrip属性,它几乎等同于ContextMenuStrip属性的DataGridViewRow类。The following code example demonstrates how to use the DataGridViewColumn.ContextMenuStrip property, which is nearly identical to the ContextMenuStrip property of the DataGridViewRow class. 此代码示例中提供一个更大的代码示例摘自DataGridViewColumn类。This code example is part of a larger code example provided in the DataGridViewColumn class.

ToolStripMenuItem^ toolStripItem1;
void AddContextMenu()
{
   toolStripItem1->Text = L"Redden";
   toolStripItem1->Click += gcnew EventHandler( this, &DataGridViewColumnDemo::toolStripItem1_Click );
   System::Windows::Forms::ContextMenuStrip^ strip = gcnew System::Windows::Forms::ContextMenuStrip;
   IEnumerator^ myEnum = dataGridView->Columns->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      DataGridViewColumn^ column = safe_cast<DataGridViewColumn^>(myEnum->Current);
      column->ContextMenuStrip = strip;
      column->ContextMenuStrip->Items->Add( toolStripItem1 );
   }
}

DataGridViewCellEventArgs^ mouseLocation;

// Change the cell's color.
void toolStripItem1_Click( Object^ /*sender*/, EventArgs^ /*args*/ )
{
   dataGridView->Rows[ mouseLocation->RowIndex ]->Cells[ mouseLocation->ColumnIndex ]->Style->BackColor = Color::Red;
}


// Deal with hovering over a cell.
void dataGridView_CellMouseEnter( Object^ /*sender*/, DataGridViewCellEventArgs^ location )
{
   mouseLocation = location;
}


ToolStripMenuItem toolStripItem1 = new ToolStripMenuItem();

private void AddContextMenu()
{
    toolStripItem1.Text = "Redden";
    toolStripItem1.Click += new EventHandler(toolStripItem1_Click);
    ContextMenuStrip strip = new ContextMenuStrip();
    foreach (DataGridViewColumn column in dataGridView.Columns)
    {

        column.ContextMenuStrip = strip;
        column.ContextMenuStrip.Items.Add(toolStripItem1);
    }
}

private DataGridViewCellEventArgs mouseLocation;

// Change the cell's color.
private void toolStripItem1_Click(object sender, EventArgs args)
{
    dataGridView.Rows[mouseLocation.RowIndex]
        .Cells[mouseLocation.ColumnIndex].Style.BackColor
        = Color.Red;
}

// Deal with hovering over a cell.
private void dataGridView_CellMouseEnter(object sender,
    DataGridViewCellEventArgs location)
{
    mouseLocation = location;
}
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
        column.ContextMenuStrip.Items.Add(toolStripItem1)
    Next
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

注解

此属性指示用户右键单击该行,除非时显示的快捷菜单DataGridView控制CellContextMenuStripNeeded事件或行ContextMenuStrip属性将替代被单击的特定单元格的快捷菜单。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.

DataGridView控件DataSource属性设置或其VirtualMode属性是true,获取的值ContextMenuStrip属性将引发RowContextMenuStripNeeded控件的事件,并返回的值DataGridViewRowContextMenuStripNeededEventArgs.ContextMenuStrip为属性指定在事件处理程序。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. 如果没有为事件处理程序,则获取的值ContextMenuStrip属性返回以前指定的值或其默认值为nullIf 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.

处理RowContextMenuStripNeeded事件时,可以主要使用大量的数据以避免性能损失,设置行时ContextMenuStrip的多个行的值。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. 有关详细信息,请参阅缩放 Windows 窗体 DataGridView 控件的最佳做法For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.

更改此属性将引发RowContextMenuStripChanged对所具有的事件DataGridView,如果存在一个。Changing this property raises the RowContextMenuStripChanged event on the owning DataGridView, if one exists.

如果当前行共享的使用GetContextMenuStrip方法,而无需获取此属性的值。If the current row is shared, use the GetContextMenuStrip method rather than getting the value of this property.

适用于

另请参阅