DataGridViewCellValidatingEventArgs 类      
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为 CellValidating 控件的 DataGridView 事件提供数据。
public ref class DataGridViewCellValidatingEventArgs : System::ComponentModel::CancelEventArgspublic class DataGridViewCellValidatingEventArgs : System.ComponentModel.CancelEventArgstype DataGridViewCellValidatingEventArgs = class
    inherit CancelEventArgsPublic Class DataGridViewCellValidatingEventArgs
Inherits CancelEventArgs- 继承
示例
下面的代码示例处理 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使用 属性确定当前值。 若要确定当前单元格的状态,请使用 RowIndex 和 ColumnIndex 属性通过 DataGridView.Rows 集合访问单元格。 若要取消更改,请将 Cancel 属性设置为 true。
在数据绑定模式下取消此事件时,新值不会推送到基础数据源。 在虚拟模式下取消此事件时,不会引发该 DataGridView.CellValuePushed 事件。
属性
| Cancel | 获取或设置指示是否应取消事件的值。(继承自 CancelEventArgs) | 
| ColumnIndex | 获取需要验证的单元格的列索引。 | 
| FormattedValue | 获取需要验证的单元格的已设置格式的内容。 | 
| RowIndex | 获取需要验证的单元格的行索引。 | 
方法
| Equals(Object) | 确定指定对象是否等于当前对象。(继承自 Object) | 
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) |