With the next Q3 2009 official release we are introducing a significant improvement to the validation mechanism of the RadGridView control. If we compare the new validation to the previous versions we add another validation layer (Data Layer) and refactoring of the related events. In a series of blog posts I’ll try to clarify this mechanism with several small examples. I’ll start the series with validation for a single cell.
How to: Validate data on a cell (property) level with RadGridView for Silverlight and WPF
The cell validation occurs when a GridViewCell is edited and its new data is about to be committed. At this moment CellValidating event is raised and since the new value is not yet committed to the underlying data object, here is the perfect place for applying a custom UI validation. By UI validation I mean the validation that occurs within the RadGridView control, which can be more restrictive than data validation according to specific business rules. Below is a code snippet that validates property FirstName of the Person object to be longer than 3 characters:
2: this.radGridView.CellValidating += this.radGridView_CellValidating;
5: private void radGridView_CellValidating(object sender, GridViewCellValidatingEventArgs e)
7: if (e.Cell.Column.UniqueName == "FirstName")
9: if (e.NewValue.ToString().Length < 3)
11: e.IsValid = false;
12: e.ErrorMessage = "FirstName should be longer than 3 characters.";
Note: For the sake of convenience all events involved into validation are exposed to the RadGridView control.
When you set e.IsValid = false, in the Validating events this will cancel the editing process and will return focus to the invalid GridViewCell. After this UI layer validation is successful (e.IsValid = true, which is the default value), Data layer validation occurs. This is the validation which is built in the business object implementation. There are several ways to do that:
- Throw exception when incorrect value is about to be set. This results into a binding validation error and GridViewBoundColumnBase editor enters into invalid state.
1: public string FirstName
5: return this.firstName;
9: if (value.Length < 3)
11: throw new Exception("FirstName should be longer than 3 characters.");
13: this.firstName = value;
Note: Exception of any kind will result as a validation error, and exception message will appear as an error tooltip.
2. With the Silverlight platform you can take advantage from System.ComponentModel.DataAnnotations assembly. The following code snippet demonstrates how to use data annotation attributes to validate a property value (again within the property setter). It is even better when you use RIA services, since this code will be automatically added for you by the RIA Service’s code generator.
2: public string LastName
6: return this.lastName;
10: Validator.ValidateProperty(value, new ValidationContext(this, null, null)
12: MemberName = "LastName"
14: this.lastName = value;
Important: At this layer when data is validated and no error has occurred, the new data is committed to the data object.
After UI and Data layer validation are finished the CellValidated event occurs and here you can add custom actions. For example to change the visual state of the editor to invalid (when custom editor is used and it has no default invalid state associated with binding errors). The example shows how to change the editor background:
1: private void radGridView_CellValidated(object sender, GridViewCellValidatedEventArgs e)
3: ((Control) e.EditorElement).Background = new SolidColorBrush(Colors.Yellow);
You can play with this sample application in this blog post.Here it is.
Here is the complete source code of the example.
Next time we will discuss row/object level validation. Stay tuned.