Share via


DataGridViewDataErrorEventArgs Kelas

Definisi

Menyediakan data untuk peristiwa tersebut DataError .

public ref class DataGridViewDataErrorEventArgs : System::Windows::Forms::DataGridViewCellCancelEventArgs
public class DataGridViewDataErrorEventArgs : System.Windows.Forms.DataGridViewCellCancelEventArgs
type DataGridViewDataErrorEventArgs = class
    inherit DataGridViewCellCancelEventArgs
Public Class DataGridViewDataErrorEventArgs
Inherits DataGridViewCellCancelEventArgs
Warisan

Contoh

Contoh kode berikut menunjukkan cara menanggapi informasi yang disediakan oleh DataGridViewDataErrorEventArgs kelas . Contoh ini adalah bagian dari contoh yang lebih besar yang tersedia dalam DataGridViewComboBoxColumn topik gambaran umum kelas.

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

Keterangan

Menangani DataError peristiwa memungkinkan Anda menangani pengecualian yang dilemparkan oleh kode di luar kontrol Anda (misalnya, oleh sumber data eksternal). Context Gunakan properti untuk menentukan status DataGridView pada saat pengecualian. Exception Gunakan properti untuk mengambil data pengecualian. Jika Anda ingin menangani pengecualian oleh penanganan aktivitas tambahan, atur properti ke ThrowExceptiontrue.

Properti ColumnIndex dan RowIndex biasanya menunjukkan sel tempat kesalahan data terjadi. Namun, ketika kesalahan terjadi di sumber data eksternal, sumber data mungkin tidak menyediakan kolom tempat kesalahan terjadi. Dalam hal ini, ColumnIndex properti biasanya menunjukkan kolom sel saat ini pada saat kesalahan.

Konstruktor

DataGridViewDataErrorEventArgs(Exception, Int32, Int32, DataGridViewDataErrorContexts)

Menginisialisasi instans baru kelas DataGridViewDataErrorEventArgs.

Properti

Cancel

Mendapatkan atau menetapkan nilai yang menunjukkan apakah acara harus dibatalkan.

(Diperoleh dari CancelEventArgs)
ColumnIndex

Mendapatkan indeks kolom sel tempat peristiwa terjadi.

(Diperoleh dari DataGridViewCellCancelEventArgs)
Context

Mendapatkan detail tentang status DataGridView kapan kesalahan terjadi.

Exception

Mendapatkan pengecualian yang mewakili kesalahan.

RowIndex

Mendapatkan indeks baris sel tempat peristiwa terjadi.

(Diperoleh dari DataGridViewCellCancelEventArgs)
ThrowException

Mendapatkan atau menetapkan nilai yang menunjukkan apakah akan melemparkan pengecualian setelah DataGridViewDataErrorEventHandler delegasi selesai dengannya.

Metode

Equals(Object)

Menentukan apakah objek yang ditentukan sama dengan objek saat ini.

(Diperoleh dari Object)
GetHashCode()

Berfungsi sebagai fungsi hash default.

(Diperoleh dari Object)
GetType()

Mendapatkan dari instans Type saat ini.

(Diperoleh dari Object)
MemberwiseClone()

Membuat salinan dangkal dari saat ini Object.

(Diperoleh dari Object)
ToString()

Mengembalikan string yang mewakili objek saat ini.

(Diperoleh dari Object)

Berlaku untuk

Lihat juga