DataGridViewCellValidatingEventHandler 委托

定义

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

public delegate void DataGridViewCellValidatingEventHandler(System::Object ^ sender, DataGridViewCellValidatingEventArgs ^ e);
public delegate void DataGridViewCellValidatingEventHandler(object sender, DataGridViewCellValidatingEventArgs e);
public delegate void DataGridViewCellValidatingEventHandler(object? sender, DataGridViewCellValidatingEventArgs e);
type DataGridViewCellValidatingEventHandler = delegate of obj * DataGridViewCellValidatingEventArgs -> unit
Public Delegate Sub DataGridViewCellValidatingEventHandler(sender As Object, e As DataGridViewCellValidatingEventArgs)

参数

sender
Object

对事件发送方的引用。

示例

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

注解

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

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

扩展方法

GetMethodInfo(Delegate)

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

适用于

另请参阅