閱讀英文

共用方式為


DataGridViewCellValidatingEventHandler 代理人

定義

表示方法,處理 CellValidating 控制項的 DataGridView 事件。

C#
public delegate void DataGridViewCellValidatingEventHandler(object sender, DataGridViewCellValidatingEventArgs e);
C#
public delegate void DataGridViewCellValidatingEventHandler(object? sender, DataGridViewCellValidatingEventArgs e);

參數

sender
Object

事件傳送者的參考。

範例

下列程式碼範例會處理 CellValidating 事件,以確保使用者只輸入正整數。 此範例是參考主題中較大範例的 VirtualMode 一部分。

C#
private void dataGridView1_CellValidating(object sender,
    DataGridViewCellValidatingEventArgs e)
{
    dataGridView1.Rows[e.RowIndex].ErrorText = "";
    int newInteger;

    // Don't try to validate the 'new row' until finished 
    // editing since there
    // is not any point in validating its initial value.
    if (dataGridView1.Rows[e.RowIndex].IsNewRow) { return; }
    if (!int.TryParse(e.FormattedValue.ToString(),
        out newInteger) || newInteger < 0)
    {
        e.Cancel = true;
        dataGridView1.Rows[e.RowIndex].ErrorText = "the value must be a non-negative integer";
    }
}

備註

CellValidating 儲存格失去輸入焦點,啟用內容驗證時,就會發生此事件。 取消此事件會取消目前儲存格的變更。 當此事件在資料系結模式中取消時,新的值不會推送至基礎資料來源。 當此事件在虛擬模式中取消時, CellValuePushed 將不會引發事件。

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

另請參閱