DataRow.HasErrors 屬性

定義

取得值,指出資料列中是否有錯誤。

public:
 property bool HasErrors { bool get(); };
public bool HasErrors { get; }
member this.HasErrors : bool
Public ReadOnly Property HasErrors As Boolean

屬性值

如果資料列含有錯誤,則為 true,否則為 false

範例

下列範例會使用 HasErrors 來尋找錯誤。 如果數據列有錯誤,此方法會 GetColumnsInError 傳回數據行陣列,並接著可以解決這些錯誤。 接著會 ClearErrors 呼叫 方法,以清除所有錯誤。

private void GetAllErrs(DataRow row)
{
    // Declare an array variable for DataColumn objects.
    DataColumn[] colArr;
    // If the Row has errors, check use GetColumnsInError.
    if(row.HasErrors)
    {
        // Get the array of columns in error.
        colArr = row.GetColumnsInError();
        for(int i = 0; i < colArr.Length; i++)
        {
            // Insert code to fix errors on each column.
            Console.WriteLine(colArr[i].ColumnName);
        }
        // Clear errors after reconciling.
        row.ClearErrors();
    }
}
Private Sub GetAllErrs(ByVal row As DataRow)
    ' Declare an array variable for DataColumn objects.
    Dim colArr() As DataColumn 

    ' If the Row has errors, check use GetColumnsInError.
    Dim i As Integer
    If row.HasErrors Then 
       ' Get the array of columns in error.
       colArr = row.GetColumnsInError()
       For i = 0 to colArr.Length - 1
          ' Insert code to fix errors on each column.
          Console.WriteLine(colArr(i).ColumnName)
       Next i

    ' Clear errors after reconciling.
    row.ClearErrors()
    End If
End Sub

備註

HasErrorstrue如果資料欄取任何DataColumn物件包含錯誤,或 RowErrorDataRow 屬性不是空字串,則會傳回 。

驗證資料時,您可以在資料列中的任何資料行上設定錯誤。 在控件中 System.Windows.Forms.DataGrid 顯示這類數據行時,會以紅色驚嘆號標示,以向使用者發出錯誤數據行的訊號。

使用 SetColumnError 在任何數據行上設定錯誤。

GetColumnError使用和 GetColumnsInError 方法可傳回發生錯誤的數據行。

方法 ClearErrors 會清除資料列的所有錯誤。

適用於

另請參閱