In my partial class containing the DataSet event I have the following:
protected override void OnColumnChanging(System.Data.DataColumnChangeEventArgs e)
{
switch (e.Column.ColumnName)
{
case "ColumnA":
{
int value = GetValue(e.ProposedValue.ToString());
if (value == -1)
{
e.Row.SetColumnError("ColumnA", string.Format("ColumnA could not map [{0}] to a valid value", e.ProposedValue));
//e.ProposedValue = "";
}
break;
}
base.OnColumnChanging(e);
}
When I check for errors and get the column errors for my rows I see the appropriate message when GetValue(…) returns -1. I also see that the column in that has the bad data still contains that bad value. I was under the impression that calling SetColumnError(…) would reject the change made to that column (ColumnA) as per: How to: Validate Data During Column Changes
Reject the proposed value by setting the column error (SetColumnError)
from within the column-changing event handler.
So when I try to do something like the following:
TypedDataSet set = new TypedDataSet();
TypedDataTable.TypedDataRow row = set.TypedDataTable.NewRow();
row.ColumnA = "Bad Data";
set.TypedDataTable.AddTypedDataRow(row);
I’ll see the validation code execute, but the value of ColumnA retains: “Bad Data”. If I go as far as setting e.ProposedValue = null I can see the value change.
Update
Add event handlers for either RowsChanging or ColumnsChanging also produce similar results.
public override void BeginInit()
{
base.BeginInit();
TypedRowChanging += new TypedRowChangeEventHandler(TypedDataTable_TypedRowChanging);
ColumnChanging += new DataColumnChangeEventHandler(TypedDataTable_ColumnChanging);
}
Code in both of the event handlers is trivial and will call e.Row.SetColumnError(“ColumnA”, “some error”). So my original question remains:
What should be happening the in case where a column error is set on a column? Should it retain value, become null, 42?
After much testing and work I’ve come to the conclusion that the column retains the value when a column error is set on a specific column. My current solution is to check for errors on the entire DataSet and then proceed to aggregate those errors into a format for error handling
After this I check if my error container has any data in it. I suppose it makes sense for the column to retain value so that the programmer has access to that value and can include that it in the call to SetColumnError(…).