DataGridViewCellValidatingEventHandler 委托

定义

表示将处理 CellValidating 控件的 DataGridView 事件的方法。

C#
public delegate void DataGridViewCellValidatingEventHandler(object sender, DataGridViewCellValidatingEventArgs e);
C#
public delegate void DataGridViewCellValidatingEventHandler(object? sender, DataGridViewCellValidatingEventArgs e);

参数

sender
Object

对事件发送方的引用。

示例

下面的代码示例处理 CellValidating 事件,以确保用户只输入正整数。 此示例是参考主题中提供的更大示例的 VirtualMode 一部分。

C#
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";
    }
}

注解

CellValidating 单元格失去输入焦点并启用内容验证时,会发生 该事件。 取消此事件将取消对当前单元格所做的更改。 在数据绑定模式下取消此事件时,不会将新值推送到基础数据源。 在虚拟模式下取消此事件时,不会引发该 CellValuePushed 事件。

创建 DataGridViewCellValidatingEventHandler 委托时,需要标识将处理该事件的方法。 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。 有关事件处理程序委托的详细信息,请参阅 处理和引发事件

扩展方法

GetMethodInfo(Delegate)

获取指示指定委托表示的方法的对象。

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅