DataGridView.DataError イベント

定義

外部のデータ解析または検証操作が例外をスローした場合、またはデータ ソースへのデータのコミットが失敗した場合に発生します。

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イベントを使用すると、データ処理操作中にコントロールによって呼び出されるコードでスローされた例外を処理できます。

イベントを処理する方法の詳細については、次を参照してください。処理とイベントの発生します。

Note

このイベントに関連付けられているオブジェクトの DataGridViewDataErrorEventArgs プロパティと RowIndex プロパティはColumnIndex、通常、データ エラーが発生したセルを示します。 ただし、外部データ ソースでエラーが発生した場合、データ ソースはエラーが発生した列を提供しない可能性があります。 この場合、 プロパティは ColumnIndex 通常、エラー発生時の現在のセルの列を示します。

適用対象

こちらもご覧ください