DataGridView.DataError 이벤트

정의

외부 데이터 구문 분석 또는 유효성 검사 작업에서 예외가 throw되거나 데이터 소스에 데이터를 커밋하려는 시도가 실패한 경우 발생합니다.

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

이벤트 유형

예제

다음 코드 예제에서는 이벤트 처리기를 DataError 보여 줍니다. 이 예제는에서 사용할 수 있는 보다 큰 예제의 일부는 DataGridViewComboBoxColumn 클래스 개요 항목입니다.

private:
    void DataGridView1_DataError(Object^ sender, DataGridViewDataErrorEventArgs^ anError)
    {

        MessageBox::Show("Error happened " + anError->Context.ToString());

        if (anError->Context == DataGridViewDataErrorContexts::Commit)
        {
            MessageBox::Show("Commit error");
        }
        if (anError->Context == DataGridViewDataErrorContexts::CurrentCellChange)
        {
            MessageBox::Show("Cell change");
        }
        if (anError->Context == DataGridViewDataErrorContexts::Parsing)
        {
            MessageBox::Show("parsing error");
        }
        if (anError->Context == DataGridViewDataErrorContexts::LeaveControl)
        {
            MessageBox::Show("leave control error");
        }

        if (dynamic_cast<ConstraintException^>(anError->Exception) != nullptr)
        {
            DataGridView^ view = (DataGridView^)sender;
            view->Rows[anError->RowIndex]->ErrorText = "an error";
            view->Rows[anError->RowIndex]->Cells[anError->ColumnIndex]->ErrorText = "an error";

            anError->ThrowException = false;
        }
    }
private void DataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs anError)
{

    MessageBox.Show("Error happened " + anError.Context.ToString());

    if (anError.Context == DataGridViewDataErrorContexts.Commit)
    {
        MessageBox.Show("Commit error");
    }
    if (anError.Context == DataGridViewDataErrorContexts.CurrentCellChange)
    {
        MessageBox.Show("Cell change");
    }
    if (anError.Context == DataGridViewDataErrorContexts.Parsing)
    {
        MessageBox.Show("parsing error");
    }
    if (anError.Context == DataGridViewDataErrorContexts.LeaveControl)
    {
        MessageBox.Show("leave control error");
    }

    if ((anError.Exception) is ConstraintException)
    {
        DataGridView view = (DataGridView)sender;
        view.Rows[anError.RowIndex].ErrorText = "an error";
        view.Rows[anError.RowIndex].Cells[anError.ColumnIndex].ErrorText = "an error";

        anError.ThrowException = false;
    }
}
Private Sub DataGridView1_DataError(ByVal sender As Object, _
ByVal e As DataGridViewDataErrorEventArgs) _
Handles DataGridView1.DataError

    MessageBox.Show("Error happened " _
        & e.Context.ToString())

    If (e.Context = DataGridViewDataErrorContexts.Commit) _
        Then
        MessageBox.Show("Commit error")
    End If
    If (e.Context = DataGridViewDataErrorContexts _
        .CurrentCellChange) Then
        MessageBox.Show("Cell change")
    End If
    If (e.Context = DataGridViewDataErrorContexts.Parsing) _
        Then
        MessageBox.Show("parsing error")
    End If
    If (e.Context = _
        DataGridViewDataErrorContexts.LeaveControl) Then
        MessageBox.Show("leave control error")
    End If

    If (TypeOf (e.Exception) Is ConstraintException) Then
        Dim view As DataGridView = CType(sender, DataGridView)
        view.Rows(e.RowIndex).ErrorText = "an error"
        view.Rows(e.RowIndex).Cells(e.ColumnIndex) _
            .ErrorText = "an error"

        e.ThrowException = False
    End If
End Sub

설명

이벤트를 DataError 사용하면 데이터 처리 작업 중에 컨트롤에서 호출하는 코드에서 throw된 예외를 처리할 수 있습니다.

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

참고

이 이벤트와 연결된 개체의 DataGridViewDataErrorEventArgsRowIndex 속성은 ColumnIndex 일반적으로 데이터 오류가 발생한 셀을 나타냅니다. 하지만 외부 데이터 원본에서 오류가 발생, 데이터 원본을 제공할 수 없습니다 오류가 발생 한 열. 이 경우에 ColumnIndex 속성 오류 시 일반적으로 현재 셀의 열을 나타냅니다.

적용 대상

추가 정보