共用方式為


DataGridViewRow.ContextMenuStrip 屬性

定義

取得或設定資料列的捷徑功能表。

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; }
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 相關聯。 預設為 null

例外狀況

設定這個屬性時,資料列是位於 DataGridView 控制項中,而且是共用的資料列。

範例

下列程式代碼範例示範如何使用 DataGridViewColumn.ContextMenuStrip 屬性,這與 類別的 DataGridViewRow 屬性幾乎相同ContextMenuStrip。 此程式代碼範例是 類別中提供的較大程式碼範例的 DataGridViewColumn 一部分。

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 屬性會覆寫所單擊特定儲存格的快捷方式選單。

DataGridView設定控件DataSource屬性或其VirtualMode屬性為 true時,取得 屬性的值ContextMenuStripRowContextMenuStripNeeded引發控件的事件,並傳回DataGridViewRowContextMenuStripNeededEventArgs.ContextMenuStrip事件處理程式中指定的屬性值。 如果沒有事件的處理程式,取得 屬性的值會傳回先前指定的值 ContextMenuStrip 或預設值 null

RowContextMenuStripNeeded處理事件主要是在處理大量數據時很有用,以避免在設定多個數據列的數據列ContextMenuStrip值時造成效能負面影響。 如需詳細資訊,請參閱 縮放 Windows Form DataGridView 控制項的最佳做法

如果存在, RowContextMenuStripChanged 則變更這個屬性會在擁有 DataGridView時引發 事件。

如果共用目前的數據列,請使用 GetContextMenuStrip 方法,而不是取得此屬性的值。

適用於

另請參閱