DataGridViewCellValidatingEventArgs 類別

定義

提供 CellValidating 控制項的 DataGridView 事件的資料。

public ref class DataGridViewCellValidatingEventArgs : System::ComponentModel::CancelEventArgs
public class DataGridViewCellValidatingEventArgs : System.ComponentModel.CancelEventArgs
type DataGridViewCellValidatingEventArgs = class
    inherit CancelEventArgs
Public Class DataGridViewCellValidatingEventArgs
Inherits CancelEventArgs
繼承
DataGridViewCellValidatingEventArgs

範例

下列程式碼範例會處理 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

備註

當新值無效時,事件 DataGridView.CellValidating 可讓您取消目前儲存格的變更。 FormattedValue使用 屬性來判斷目前的值。 若要判斷目前儲存格的狀態,請使用 RowIndexColumnIndex 屬性,透過 DataGridView.Rows 集合存取儲存格。 若要取消變更,請將 Cancel 屬性設定為 true

在資料系結模式中取消此事件時,不會將新的值推送至基礎資料來源。 當此事件在虛擬模式中取消時, DataGridView.CellValuePushed 將不會引發事件。

屬性

Cancel

取得或設定值,這個值表示是否應該取消事件。

(繼承來源 CancelEventArgs)
ColumnIndex

取得需要驗證的儲存格之資料行索引。

FormattedValue

取得需要驗證的儲存格之格式化內容。

RowIndex

取得需要驗證的儲存格之資料列索引。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱