Changing the Value Property
The first stage of a source value update – changing the ItemCell.Value property – includes four main steps: converting the value to a data type, raising the CellValueChanging event, returning errors, and executing the ItemCellValidators. These steps are detailed below:
Step 1: Converting the value to a data type
In the first step, the value provided to the ItemCell.Value property is converted to the data type defined in the DataType property of the corresponding column.
Step 2: Raising the CellValueChanging event
In the second step, the CellValueChanging event is raised and passes the CellValueChangingEventArgs object to an event handler. The event handler includes the following event argument values:
• IsError: True if the conversion performed in step 1 has failed; otherwise, False.
• ConversionException: An exception raised during a conversion; otherwise, a null value.
• NonConvertedValue: The original (pre-conversion) value of the ItemCell.Value property.
• ProposedValue: A converted value if conversion in step 1 has succeeded; otherwise, the previous value of the ItemCell.Value property.
In the event handler you can:
• Check a proposed value (ProposedValue or NonConvertedValue arguments) and report an error by setting the IsError argument to True. You can also set the ErrorContent argument to a description of the error.
Alternatively, you can suppress a conversion error by setting the IsError argument to False (note that IsError is set to True on event entry in this case).
• Provide a corrected value (different from an input value) for the ItemCell.Value property by setting another value to the ProposedValue argument.
Step 3: Returning Errors
If the IsError argument is True on the CellValueChanging event exit, the process is stopped and the CellErrorInfo object being returned by the ErrorInfo property gets the following property values from event arguments:
ErrorInfo property |
Value or value source |
HasError |
CellValueChangingEventArgs.IsError |
ErrorContent |
CellValueChangingEventArgs.ErrorContent |
ErrorException |
CellValueChangingEventArgs.ConversionException |
MustFixError |
True |
IsDataSourceError |
False |
Step 4: Executing the ItemCellValidator
If the IsError argument is True on the CellValueChanging event exit, the CellValidator-derived objects from the ItemCellValidators collection are executed. CellValidator is an abstract class with the Validate abstract method that should be implemented in a custom CellValidator-derived class and return a CellValidationResult object that indicates whether a value passed a validation. If at least one of the validations fails, that is CellValidationResult returned by the Validate method has the IsValid property set to False, the process is stopped and the CellErrorInfo object returned by the ErrorInfo property gets the following property values from CellValidationResult:
ErrorInfo property |
Value or value source |
HasError |
not CellValidationResult.IsValid |
ErrorContent |
CellValidationResult.ErrorContent |
ErrorException |
Null |
MustFixError |
True |
IsDataSourceError |
False |
A validator that checks an input value for positivity may look similar to the following:
Public Class PositiveValueValidator
Inherits CellValidator
Public Overloads Overrides Function Validate(ByVal value As Object, ByVal cell As ValueCellBase, ByVal cultureInfo As CultureInfo) As CellValidationResult
If TypeOf value Is Integer Then
If CInt(value) <= 0 Then
Return New CellValidationResult(False, "Value must be greater than 0.")
End If
Else
Return New CellValidationResult(False, "Invalid value type")
End If
Return CellValidationResult.ValidResult
End Function
End Class
• C#
public class PositiveValueValidator : CellValidator
{
public override CellValidationResult Validate(object value, ValueCellBase cell, CultureInfo cultureInfo)
{
if (value is int)
{
if ((int)value <= 0)
return new CellValidationResult(false, "Value must be greater than 0.");
}
else
return new CellValidationResult(false, "Invalid value type");
return CellValidationResult.ValidResult;
}
}
There is a special validator class named CellValidationRuleWrapper that allows using a validator derived from the System.Windows.Controls.ValidationRule used in WPF Binding.
|