DataGridViewCellValueEventHandler 代理人

定義

表示將處理 CellValueNeededCellValuePushed 事件或 DataGridView 事件的方法。

C#
public delegate void DataGridViewCellValueEventHandler(object sender, DataGridViewCellValueEventArgs e);
C#
public delegate void DataGridViewCellValueEventHandler(object? sender, DataGridViewCellValueEventArgs e);

參數

sender
Object

事件的來源。

範例

下列程式碼範例會 CellValuePushed 處理 事件,以在資料存放區物件中儲存更新和新專案。 此範例是參考主題中較大範例的 DataGridView.VirtualMode 一部分。

C#
#region "data store maintance"
const int initialValue = -1;

private void dataGridView1_CellValueNeeded(object sender,
    DataGridViewCellValueEventArgs e)
{
    if (store.ContainsKey(e.RowIndex))
    {
        // Use the store if the e value has been modified 
        // and stored.            
        e.Value = store[e.RowIndex];
    }
    else if (newRowNeeded && e.RowIndex == numberOfRows)
    {
        if (dataGridView1.IsCurrentCellInEditMode)
        {
            e.Value = initialValue;
        }
        else
        {
            // Show a blank value if the cursor is just resting
            // on the last row.
            e.Value = String.Empty;
        }
    }
    else
    {
        e.Value = e.RowIndex;
    }
}

private void dataGridView1_CellValuePushed(object sender,
    DataGridViewCellValueEventArgs e)
{
    store.Add(e.RowIndex, int.Parse(e.Value.ToString()));
}
#endregion

private Dictionary<int, int> store = new Dictionary<int, int>();

備註

使用此委派在 控制項中實作 DataGridView 虛擬模式。 如需虛擬模式的詳細資訊,請參閱Windows Forms DataGridView 控制項中的虛擬模式

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

另請參閱