閱讀英文

共用方式為


DataGridViewRowContextMenuStripNeededEventHandler 代理人

定義

代表將處理 RowContextMenuStripNeededDataGridView 事件的方法。

C#
public delegate void DataGridViewRowContextMenuStripNeededEventHandler(object sender, DataGridViewRowContextMenuStripNeededEventArgs e);
C#
public delegate void DataGridViewRowContextMenuStripNeededEventHandler(object? sender, DataGridViewRowContextMenuStripNeededEventArgs e);

參數

sender
Object

事件的來源。

範例

在下列程式代碼範例中,會 RowContextMenuStripNeeded 處理 事件,以根據員工的職稱提供 ContextMenuStrip 。 在此範例中,有兩個操作功能表,一個用於經理,另一個用於所有其他員工。

C#
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;
}

備註

RowContextMenuStripNeeded只有在設定控件DataSource屬性或其 VirtualMode 屬性為 true時,DataGridView才會發生此事件。 RowContextMenuStripNeeded當您想要顯示數據列目前狀態或包含的值所決定的快捷方式功能表時,處理事件很有用。

當您處理 RowContextMenuStripNeeded 事件時,每當使用者以滑鼠右鍵按下數據列時,就會顯示您在處理程式中指定的快捷方式功能表,除非 CellContextMenuStripNeeded 覆寫所單擊特定儲存格的快捷方式選單。

RowContextMenuStripNeeded每當以程式設計方式擷取屬性的值DataGridViewRow.ContextMenuStrip,或使用者以滑鼠右鍵按兩下數據列時,也會發生此事件。

您可以使用 DataGridViewRowContextMenuStripNeededEventArgs.RowIndex 屬性來判斷數據列的狀態或其包含的值,並使用這項資訊來變更或修改 DataGridViewRowContextMenuStripNeededEventArgs.ContextMenuStrip 屬性。 這個屬性會使用事件值所覆寫的數據列 ContextMenuStrip 屬性值初始化。

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

如需如何處理事件的詳細資訊,請參閱 處理和引發事件

建立 DataGridViewRowContextMenuStripNeededEventHandler 委派時,必須識別處理事件的方法。 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。 除非您移除委派,否則每當事件發生時就會呼叫事件處理常式。 如需事件處理程式委派的詳細資訊,請參閱 處理和引發事件

擴充方法

GetMethodInfo(Delegate)

取得表示特定委派所代表之方法的物件。

適用於

產品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另請參閱