DataGridViewRow.ContextMenuStrip 屬性

定義

取得或設定資料列的捷徑功能表。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,與目前的 DataGridViewRow 相關聯。The 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 屬性,這幾乎與 DataGridViewRow 類別的 ContextMenuStrip 屬性相同。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 Form DataGridView 控制項的最佳做法For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.

變更此屬性會在擁有的 DataGridView上引發 RowContextMenuStripChanged 事件(如果有的話)。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.

適用於

另請參閱