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 . 이 이벤트를 사용하여 행의 모든 값에 대한 유효성 검사를 수행합니다. 행이 유효성 검사를 통과하지 못하면 속성을 trueCancelEventArgs.Cancel 설정합니다. 이 이벤트를 RowValidated취소하면 , Control.ValidatingControl.Validated 이벤트가 발생하지 않으며 사용자가 잘못된 행을 벗어나지 못하게 하고 행이 데이터 바인딩 모드의 외부 데이터 원본에 저장되지 않도록 방지합니다.

이벤트를 처리 하는 방법에 대 한 자세한 내용은 참조 하세요. 이벤트 처리 및 발생합니다.

적용 대상

추가 정보