DataGridViewCellValidatingEventHandler DataGridViewCellValidatingEventHandler DataGridViewCellValidatingEventHandler DataGridViewCellValidatingEventHandler Delegate

定义

表示将处理 CellValidating 控件的 DataGridView 事件的方法。Represents the method that will handle the CellValidating event of a DataGridView control.

public delegate void DataGridViewCellValidatingEventHandler(System::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 Object Object Object

对事件发送方的引用。A reference to the event sender.

继承
DataGridViewCellValidatingEventHandlerDataGridViewCellValidatingEventHandlerDataGridViewCellValidatingEventHandlerDataGridViewCellValidatingEventHandler

示例

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

注解

CellValidating事件发生时在单元格失去输入的焦点,启用内容验证。The CellValidating event occurs when a cell loses input focus, enabling content validation. 取消此事件取消对当前单元格的更改。Canceling this event cancels the changes to the current cell. 取消此事件时在数据绑定模式下,新值未推送到基础数据源。When this event is canceled in data-bound mode, the new value is not pushed to the underlying data source. 当处于虚拟模式下,取消此事件CellValuePushed不会引发事件。When this event is canceled in virtual mode, the CellValuePushed event will not be raised.

创建 DataGridViewCellValidatingEventHandler 委托时,需要标识将处理该事件的方法。When you create a DataGridViewCellValidatingEventHandler delegate, you identify the method that will handle the event. 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。To associate the event with your event handler, add an instance of the delegate to the event. 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。The event handler is called whenever the event occurs, unless you remove the delegate. 有关事件处理程序委托的详细信息,请参阅处理和引发事件For more information about event-handler delegates, see Handling and Raising Events.

扩展方法

GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate)

获取指示指定委托表示的方法的对象。Gets an object that represents the method represented by the specified delegate.

适用于

另请参阅