Blazor DataGrid Example - Custom Validation
Order ID | Customer ID | Employee ID | Freight | Ship City |
|---|
| 10241 | VINET | 1 | $30.99 | Reims |
| 10242 | TOMSP | 2 | $50.52 | Munster |
| 10243 | HANAR | 3 | $32.28 | Rio de Janeir |
| 10244 | VICTE | 4 | $22.90 | Lyon |
| 10245 | VINET | 5 | $30.99 | Reims |
| 10246 | TOMSP | 6 | $50.52 | Munster |
| 10247 | HANAR | 7 | $32.28 | Rio de Janeir |
| 10248 | VICTE | 8 | $22.90 | Lyon |
| 10249 | VINET | 9 | $30.99 | Reims |
| 10250 | TOMSP | 10 | $50.52 | Munster |
| 10251 | HANAR | 11 | $32.28 | Rio de Janeir |
| 10252 | VICTE | 12 | $22.90 | Lyon |
This sample demonstrates how to implement custom validation in the DataGrid using data annotations.
The Blazor DataGrid supports custom validation through data annotations. To use custom validation, import the System.ComponentModel.DataAnnotations namespace and create a custom validation class that extends ValidationAttribute. Override the IsValid method to implement your validation logic.
In this demo, the following custom validations are implemented:
- A CustomValidationFreight class validates that the Freight value is between 1 and 1000.
- A CustomValidationEmployeeID class validates that the EmployeeID value is greater than 0.
When users enter invalid data, error messages are displayed automatically. Try adding a new record with empty or invalid values to see the validation messages.
For more detailed information about custom validation, refer to the custom validation documentation.