閱讀英文

共用方式為


DataGridViewCellContextMenuStripNeededEventHandler 代理人

定義

表示將處理 CellContextMenuStripNeededDataGridView 事件的方法。

C#
public delegate void DataGridViewCellContextMenuStripNeededEventHandler(object sender, DataGridViewCellContextMenuStripNeededEventArgs e);
C#
public delegate void DataGridViewCellContextMenuStripNeededEventHandler(object? sender, DataGridViewCellContextMenuStripNeededEventArgs e);

參數

sender
Object

事件的來源。

範例

下列程式代碼範例會使用 DataGridViewCellContextMenuStripNeededEventArgs 來設定操作功能表,而不需取消共享數據列。

C#
private ToolStripMenuItem wholeTable = new ToolStripMenuItem();
private ToolStripMenuItem lookUp = new ToolStripMenuItem();
private ContextMenuStrip strip;
private string cellErrorText;

private void dataGridView1_CellContextMenuStripNeeded(object sender,
    DataGridViewCellContextMenuStripNeededEventArgs e)
{
    cellErrorText = String.Empty;

    if (strip == null)
    {
        strip = new ContextMenuStrip();
        lookUp.Text = "Look Up";
        wholeTable.Text = "See Whole Table";
        strip.Items.Add(lookUp);
        strip.Items.Add(wholeTable);
    }
    e.ContextMenuStrip = strip;
}

private void wholeTable_Click(object sender, EventArgs e)
{
    dataGridView1.DataSource = Populate("Select * from employees", true);
}

private DataGridViewCellEventArgs theCellImHoveringOver;

private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
    theCellImHoveringOver = e;
}

private DataGridViewCellEventArgs cellErrorLocation;

private void lookUp_Click(object sender, EventArgs e)
{
    try
    {
        dataGridView1.DataSource = Populate("Select * from employees where " +
            dataGridView1.Columns[theCellImHoveringOver.ColumnIndex].Name + " = '" +
            dataGridView1.Rows[theCellImHoveringOver.RowIndex].
            Cells[theCellImHoveringOver.ColumnIndex].Value + "'",
            true);
    }
    catch (SqlException)
    {
        cellErrorText = "Can't look this cell up";
        cellErrorLocation = theCellImHoveringOver;
    }
}

private void dataGridView1_CellErrorTextNeeded(object sender,
    DataGridViewCellErrorTextNeededEventArgs e)
{
    if (cellErrorLocation != null)
    {
        if (e.ColumnIndex == cellErrorLocation.ColumnIndex &&
            e.RowIndex == cellErrorLocation.RowIndex)
        {
            e.ErrorText = cellErrorText;
        }
    }
}

private DataTable Populate(string query, bool resetUnsharedCounter)
{
    if (resetUnsharedCounter)
    {
        ResetCounter();
    }

    // Alter the data source as necessary
    SqlDataAdapter adapter = new SqlDataAdapter(query,
        new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;" +
        "Initial Catalog=Northwind;Data Source=localhost"));

    DataTable table = new DataTable();
    table.Locale = System.Globalization.CultureInfo.InvariantCulture;
    adapter.Fill(table);
    return table;
}

private Label count = new Label();
private int unsharedRowCounter;

private void ResetCounter()
{
    unsharedRowCounter = 0;
    count.Text = unsharedRowCounter.ToString();
}

備註

CellContextMenuStripNeeded只有在設定控件DataSource屬性或其屬性為 trueDataGridView,才會VirtualMode發生此事件。

當您處理 CellContextMenuStripNeeded 事件時,每當使用者以滑鼠右鍵單擊單元格時,就會顯示您在處理程式中指定的快捷方式功能表。 當您想要顯示儲存格目前狀態或值所決定的快捷選單時,這會很有用。

CellContextMenuStripNeeded每當以程式設計方式擷取屬性的值DataGridViewCell.ContextMenuStrip,或使用者以滑鼠右鍵按兩下單元格時,也會發生此事件。

您可以使用 ColumnIndexRowIndex 屬性來判斷儲存格的狀態或值,並使用這項資訊來設定 ContextMenuStrip 屬性。 這個屬性會使用事件值覆寫的儲存格 ContextMenuStrip 屬性值初始化。

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

您也可以藉由設定數據列屬性或處理DataGridView控件的事件RowContextMenuStripNeeded,指定個別數據列ContextMenuStrip的快捷功能表,而不是個別儲存格。 單元格 ContextMenuStrip 屬性設定會覆寫數據列 ContextMenuStrip 屬性設定,而 CellContextMenuStripNeeded 事件會 RowContextMenuStripNeeded 同時覆寫事件和數據列 ContextMenuStrip 屬性設定。 不過,您可以指定 null 儲存格快捷方式選單,以防止覆寫資料列快捷方式選單。

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

建立 DataGridViewCellContextMenuStripNeededEventHandler 委派時,必須識別處理事件的方法。 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (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

另請參閱