共用方式為


DataGridViewCellValidatingEventHandler 代理人

定義

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

public delegate void DataGridViewCellValidatingEventHandler(System::Object ^ sender, DataGridViewCellValidatingEventArgs ^ e);
public delegate void DataGridViewCellValidatingEventHandler(object sender, DataGridViewCellValidatingEventArgs e);
public delegate void DataGridViewCellValidatingEventHandler(object? sender, DataGridViewCellValidatingEventArgs e);
type DataGridViewCellValidatingEventHandler = delegate of obj * DataGridViewCellValidatingEventArgs -> unit
Public Delegate Sub DataGridViewCellValidatingEventHandler(sender As Object, e As DataGridViewCellValidatingEventArgs)

參數

sender
Object

事件傳送者的參考。

範例

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

void VirtualConnector::dataGridView1_CellValidating
    (Object^ sender, DataGridViewCellValidatingEventArgs^ e)
{
    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 (!Int32::TryParse(e->FormattedValue->ToString(), 
        newInteger) || (newInteger < 0))
    {
        e->Cancel = true;
    }
}
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";
    }
}
Private Sub dataGridView1_CellValidating(ByVal sender As Object, _
    ByVal e _
    As DataGridViewCellValidatingEventArgs) _
    Handles dataGridView1.CellValidating

    Me.dataGridView1.Rows(e.RowIndex).ErrorText = ""
    Dim newInteger As Integer

    ' 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 Then Return
    If Not Integer.TryParse(e.FormattedValue.ToString(), newInteger) _
        OrElse newInteger < 0 Then

        e.Cancel = True
        Me.dataGridView1.Rows(e.RowIndex).ErrorText = "the value must be a non-negative integer"

    End If
End Sub

備註

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

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

擴充方法

GetMethodInfo(Delegate)

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

適用於

另請參閱