Tuesday, June 07, 2005
DataRow.ClearErrors doesn't quite cut it
As noted in my previous post, DataRow.ClearErrors isn't as useful as it could be, but seems to handle clearing errors quite reasonably as long as you can loop down to the DataRows in error. It's actually worse than I thought - ClearErrors will take the errors away from your row, but it doesn't trigger any events to tell anyone that the row has changed! So if you have UI bound to your DataSet, showing errors to the user, and then you call ClearErrors on the row, the component doesn't know and won't refresh itself unless you do something else. Reflector to the rescue... interestingly the property setter for DataRow.RowError and the DataRow.SetColumnError method both handle the events. Setting either a column or row error to null does remove the error. So my method to reset DataSet errors has been slightly expanded (pardon the absense of leading spaces - Blogger again...):
public static void ClearDataSetErrors(DataSet data)
{
if (data.HasErrors)
{
foreach (DataTable table in data.Tables)
{
foreach (DataRow row in table.GetErrors())
{
foreach (DataColumn column in row.GetColumnsInError())
row.SetColumnError(column, null);
row.RowError = null;
}
}
}
}