Export (0) Print
Expand All

Adding and Reading Row Error Information 

To avoid having to respond to row errors while editing values in a DataTable, you can add the error information to the row for later use. The DataRow object provides a RowError property on each row for this purpose. Adding data to the RowError property of a DataRow sets the HasErrors property of the DataRow to true. If the DataRow is part of a DataTable, and DataRow.HasErrors is true, the DataTable.HasErrors property is also true. This applies as well to the DataSet to which the DataTable belongs. When testing for errors, you can check the HasErrors property to determine if error information has been added to any rows. If HasErrors is true, you can use the GetErrors method of the DataTable to return and examine only the rows with errors, as shown in the following example.

DataTable  workTable = new DataTable("Customers");
workTable.Columns.Add("CustID", typeof(Int32));
workTable.Columns.Add("Total", typeof(Double));

workTable.RowChanged += new DataRowChangeEventHandler(OnRowChanged);

for (int i = 0; i < 10; i++)
  workTable.Rows.Add(new Object[] {i, i*100});

if (workTable.HasErrors)
{
  Console.WriteLine("Errors in Table " + workTable.TableName);

  foreach (DataRow myRow in workTable.GetErrors())
  {
    Console.WriteLine("CustID = " + myRow["CustID"]);
    Console.WriteLine(" Error = " + myRow.RowError + "\n");
  }
}

protected static void OnRowChanged(
    Object sender, DataRowChangeEventArgs args)
{
  // Check for zero values.
  if (args.Row["Total"].Equals(0D))
    args.Row.RowError = "Total cannot be 0.";
}

See Also

Show:
© 2014 Microsoft