DataGridViewCellValidatingEventArgs Class

Definition

CellValidating 控件的 DataGridView 事件提供数据。Provides data for the CellValidating event of a DataGridView control.

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

Examples

下面的代码示例处理 CellValidating 事件,以确保仅用户输入正整数。The following code example handles the CellValidating event to ensure that only positive integers are entered by the user. 此示例是 VirtualMode 参考主题中提供的一个更大示例的一部分。This example is part of a larger example available in the VirtualMode reference topic.

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

Remarks

当新值无效时,DataGridView.CellValidating 事件允许您取消对当前单元格所做的更改。The DataGridView.CellValidating event lets you cancel changes to the current cell when the new value is not valid. 使用 FormattedValue 属性来确定当前值。Use the FormattedValue property to determine the current value. 若要确定当前单元格的状态,请使用 "RowIndex" 和 "ColumnIndex" 属性通过 DataGridView.Rows 集合访问该单元。To determine the state of the current cell, use the RowIndex and ColumnIndex properties to access the cell through the DataGridView.Rows collection. 若要取消更改,请将 Cancel 属性设置为 trueTo cancel the change, set the Cancel property to true.

如果在数据绑定模式下取消此事件,则不会将新值推送到基础数据源。When this event is canceled in data-bound mode, the new value is not pushed to the underlying data source. 如果在虚拟模式下取消此事件,则不会引发 DataGridView.CellValuePushed 事件。When this event is canceled in virtual mode, the DataGridView.CellValuePushed event will not be raised.

Properties

Cancel

获取或设置指示是否应取消事件的值。Gets or sets a value indicating whether the event should be canceled.

(Inherited from CancelEventArgs)
ColumnIndex

获取需要验证的单元格的列索引。Gets the column index of the cell that needs to be validated.

FormattedValue

获取需要验证的单元格的已设置格式的内容。Gets the formatted contents of the cell that needs to be validated.

RowIndex

获取需要验证的单元格的行索引。Gets the row index of the cell that needs to be validated.

Methods

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also