DataGridView.RowValidating 事件

定义

行验证时发生。

public:
 event System::Windows::Forms::DataGridViewCellCancelEventHandler ^ RowValidating;
public event System.Windows.Forms.DataGridViewCellCancelEventHandler RowValidating;
public event System.Windows.Forms.DataGridViewCellCancelEventHandler? RowValidating;
member this.RowValidating : System.Windows.Forms.DataGridViewCellCancelEventHandler 
Public Custom Event RowValidating As DataGridViewCellCancelEventHandler 

事件类型

示例

以下代码示例使用 RowValidating 检查是否输入了有效的曲目和发布日期。

private void ValidateByRow(Object sender, 
    DataGridViewCellCancelEventArgs data) 
{
    DataGridViewRow row = 
        songsDataGridView.Rows[data.RowIndex];
    DataGridViewCell trackCell = 
        row.Cells[songsDataGridView.Columns["Track"].Index];
    DataGridViewCell dateCell = 
        row.Cells[songsDataGridView.Columns["Release Date"].Index];
    data.Cancel = !(IsTrackGood(trackCell) && IsDateGood(dateCell));
}

private Boolean IsTrackGood(DataGridViewCell cell)
{
    Int32 cellValueAsInt;
    if (cell.Value.ToString().Length == 0)
    {
        cell.ErrorText = "Please enter a track";
        songsDataGridView.Rows[cell.RowIndex].ErrorText = 
            "Please enter a track";
        return false;
    }
    else if (cell.Value.ToString().Equals("0"))
    {
        cell.ErrorText = "Zero is not a valid track";
        songsDataGridView.Rows[cell.RowIndex].ErrorText =
            "Zero is not a valid track";
        return false;
    }
    else if (!Int32.TryParse(cell.Value.ToString(), out cellValueAsInt))
    {
        cell.ErrorText = "A Track must be a number";
        songsDataGridView.Rows[cell.RowIndex].ErrorText =
            "A Track must be a number";
        return false;
    }
    return true;
}

private Boolean IsDateGood(DataGridViewCell cell) 
{
    if (cell.Value == null)
    {
        cell.ErrorText = "Missing date";
        songsDataGridView.Rows[cell.RowIndex].ErrorText = 
            "Missing date";
        return false;
    }
    else
    {
        try
        {
            DateTime.Parse(cell.Value.ToString());
        }
        catch (FormatException)
        {
            cell.ErrorText = "Invalid format";
            songsDataGridView.Rows[cell.RowIndex].ErrorText = 
                "Invalid format";

            return false;
        }
    }
    return true;
}
Private Sub ValidateByRow(ByVal sender As Object, _
    ByVal data As DataGridViewCellCancelEventArgs) _
    Handles songsDataGridView.RowValidating

    Dim row As DataGridViewRow = _
        songsDataGridView.Rows(data.RowIndex)
    Dim trackCell As DataGridViewCell = _
        row.Cells(songsDataGridView.Columns("Track").Index)
    Dim dateCell As DataGridViewCell = _
        row.Cells(songsDataGridView.Columns("Release Date").Index)
    data.Cancel = Not (IsTrackGood(trackCell) _
        AndAlso IsDateGood(dateCell))
End Sub

Private Function IsTrackGood(ByRef cell As DataGridViewCell) As Boolean

    If cell.Value.ToString().Length = 0 Then
        cell.ErrorText = "Please enter a track"
        songsDataGridView.Rows(cell.RowIndex).ErrorText = _
            "Please enter a track"
        Return False
    ElseIf cell.Value.ToString().Equals("0") Then
        cell.ErrorText = "Zero is not a valid track"
        songsDataGridView.Rows(cell.RowIndex).ErrorText = _
            "Zero is not a valid track"
        Return False
    ElseIf Not Integer.TryParse( _
        cell.Value.ToString(), New Integer()) Then
        cell.ErrorText = "A Track must be a number"
        songsDataGridView.Rows(cell.RowIndex).ErrorText = _
            "A Track must be a number"
        Return False
    End If
    Return True
End Function

Private Function IsDateGood(ByRef cell As DataGridViewCell) As Boolean

    If cell.Value Is Nothing Then
        cell.ErrorText = "Missing date"
        songsDataGridView.Rows(cell.RowIndex).ErrorText = _
            "Missing date"
        Return False
    Else
        Try
            DateTime.Parse(cell.Value.ToString())
        Catch ex As FormatException

            cell.ErrorText = "Invalid format"
            songsDataGridView.Rows(cell.RowIndex).ErrorText = _
                "Invalid format"

            Return False
        End Try
    End If
    Return True
End Function

注解

此事件类似于 Control.Validating 事件。 使用此事件对行的所有值执行验证。 如果行未通过验证,请将 CancelEventArgs.Cancel 属性设置为 true。 取消此事件可防止 RowValidated发生 、 Control.ValidatingControl.Validated 事件,还会阻止用户离开无效行,并阻止在数据绑定模式下将行保存到外部数据源。

有关如何处理事件的详细信息,请参阅 处理和引发事件

适用于

另请参阅