DataGridViewCellEventHandler 代理人

定義

表示將處理與儲存格和資料列作業有關的 DataGridView 事件之方法。

C#
public delegate void DataGridViewCellEventHandler(object sender, DataGridViewCellEventArgs e);
C#
public delegate void DataGridViewCellEventHandler(object? sender, DataGridViewCellEventArgs e);

參數

sender
Object

事件的來源。

e
DataGridViewCellEventArgs

DataGridViewCellEventArgs,其中包含事件資料。

範例

下列程式碼範例示範如何使用 CellMouseEnterCellMouseLeave 事件處理常式來判斷是否可以按一下儲存格。 此範例會更新每個 ToolTipText ,以公告目前的映射配置。 此程式碼是How to: Work with Image Columns in the Windows Forms DataGridView Control中較大範例的一部分。

C#
private void dataGridView1_CellMouseEnter(object sender,
    DataGridViewCellEventArgs e)
{
    Bitmap markingUnderMouse = (Bitmap)dataGridView1.
           Rows[e.RowIndex].
           Cells[e.ColumnIndex].Value;

    if (markingUnderMouse == blank)
    {
        dataGridView1.Cursor = Cursors.Default;
    }
    else if (markingUnderMouse == o || markingUnderMouse == x)
    {
        dataGridView1.Cursor = Cursors.No;
        ToolTip(e, true);
    }
}

private void ToolTip(DataGridViewCellEventArgs e, bool showTip)
{
    DataGridViewImageCell cell = (DataGridViewImageCell)
        dataGridView1
        .Rows[e.RowIndex].Cells[e.ColumnIndex];
    DataGridViewImageColumn imageColumn =
        (DataGridViewImageColumn)
        dataGridView1.Columns[cell.ColumnIndex];

    if (showTip)
    {
        cell.ToolTipText = imageColumn.Description;
    }
    else { cell.ToolTipText = String.Empty; }
}

private void dataGridView1_CellMouseLeave(object sender,
    DataGridViewCellEventArgs e)
{
    ToolTip(e, false);
    dataGridView1.Cursor = Cursors.Default;
}

備註

DataGridViewCellEventHandler 會處理下列 DataGridView 事件:

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

另請參閱