DataGridView.CellValidating 이벤트

정의

셀이 입력 포커스를 잃어 내용 유효성 검사가 활성화되면 발생합니다.

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

이벤트 유형

예제

다음 코드 예제에서는 처리 된 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

설명

이 이벤트를 취소 하면 현재 셀에 변경 내용을 취소 합니다. 이 이벤트는 데이터 바인딩 모드에서 취소 된 경우 새 값이 기본 데이터 원본에 푸시되 지 않습니다. 이 이벤트는 가상 모드에 취소 될 때를 CellValuePushed 이벤트 발생 하지 것입니다.

CellValidated 이벤트를 처리하여 유효성 검사 후 처리를 수행합니다.

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

적용 대상

추가 정보