DataGridViewCellValidatingEventArgs DataGridViewCellValidatingEventArgs DataGridViewCellValidatingEventArgs DataGridViewCellValidatingEventArgs Class

定义

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
继承
DataGridViewCellValidatingEventArgsDataGridViewCellValidatingEventArgsDataGridViewCellValidatingEventArgsDataGridViewCellValidatingEventArgs

示例

下面的代码示例处理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

注解

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. 若要确定当前的单元格的状态,请使用RowIndexColumnIndex属性访问通过的单元格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.

属性

Cancel Cancel Cancel Cancel

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

(Inherited from CancelEventArgs)
ColumnIndex ColumnIndex ColumnIndex ColumnIndex

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

FormattedValue FormattedValue FormattedValue FormattedValue

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

RowIndex RowIndex RowIndex RowIndex

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

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

(Inherited from Object)
GetType() GetType() GetType() GetType()

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

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

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

(Inherited from Object)
ToString() ToString() ToString() ToString()

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

(Inherited from Object)

适用于

另请参阅