DataGridViewDataErrorEventArgs.ThrowException DataGridViewDataErrorEventArgs.ThrowException DataGridViewDataErrorEventArgs.ThrowException DataGridViewDataErrorEventArgs.ThrowException Property

定義

取得或設定值,指出是否要在完成 DataGridViewDataErrorEventHandler 委派後擲出例外狀況。Gets or sets a value indicating whether to throw the exception after the DataGridViewDataErrorEventHandler delegate is finished with it.

public:
 property bool ThrowException { bool get(); void set(bool value); };
public bool ThrowException { get; set; }
member this.ThrowException : bool with get, set
Public Property ThrowException As Boolean

屬性值

如果應該擲出例外狀況則為 true,否則為 falsetrue if the exception should be thrown; otherwise, false. 預設為 falseThe default is false.

例外狀況

將這個屬性設為 true 時,Exception 屬性值為 nullWhen setting this property to true, the Exception property value is null.

範例

下列程式碼範例將示範如何ThrowException使用屬性來表示不應該擲回例外狀況。The following code example demonstrates using the ThrowException property to indicate that the exception should not be thrown. 這個範例是 [ DataGridViewComboBoxColumn類別總覽] 主題中提供之較大範例的一部分。This example is part of a larger example available in the DataGridViewComboBoxColumn class overview topic.

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

備註

false Exception DataGridViewDataErrorEventHandler如果已處理由屬性所表示的例外狀況, 而且您不想要將例外狀況傳播至另一個可能處理DataError事件的例外狀況, 請將此屬性設定為。 DataGridViewDataErrorEventHandlerSet this property to false if the DataGridViewDataErrorEventHandler has dealt with the exception represented by the Exception property, and you do not want to propagate the exception to another DataGridViewDataErrorEventHandler that may handle the DataError event.

基於偵錯工具的目的, 傳播例外狀況並檢查堆疊追蹤以取得錯誤內容的相關資訊可能會很有用。For debugging purposes, it can be useful to propagate the exception and inspect the stack trace for information regarding the context of the error.

適用於

另請參閱