You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: Data Annotation in Blazor DataGrid | Syncfusion
4
-
description: Learn how to achieve data validation with data annotations in the Syncfusion Blazor DataGrid component.
4
+
description: Learn how to use Data Annotations for validation and column metadata in Syncfusion Blazor DataGrid, including enum display and CRUD form validation.
5
5
platform: Blazor
6
6
control: DataGrid
7
7
documentation: ug
8
8
---
9
9
10
10
# Data Annotation in Blazor DataGrid
11
11
12
-
Data Annotations are used to define validation rules for model classes or properties, ensuring that data entered into an application conforms to the expected format and meets specific criteria. They also enable the display of appropriate error messages to end users.
12
+
Data annotations define validation and display rules for model classes or properties in the Syncfusion<supstyle="font-size:70%">®</sup> Blazor DataGrid. These attributes ensure that input values follow specific formats and constraints while providing clear error messages during editing operations.
13
13
14
-
In the Syncfusion<supstyle="font-size:70%">®</sup> Blazor DataGrid, Data Annotations are leveraged to automatically map these validation rules to the corresponding [GridColumn](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.GridColumn.html)properties. This integration provides seamless data validation during editing operations within the Grid.
14
+
When the DataGrid is bound to a model, data annotations automatically map to corresponding [GridColumn](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.GridColumn.html)settings. This enables built-in validation and metadata display during CRUD operations.
15
15
16
-
To enable Data Annotations for validation in a Grid, you need to refer the **System.ComponentModel.DataAnnotations** namespace in your Blazor application. Once enabled, the Grid automatically uses the data annotations applied to your model class properties to perform data validation.
16
+
To enable data annotation in the Blazor DataGrid:
17
17
18
-
The following table lists the data annotation attributes supported in the Grid:
18
+
1. Add the **System.ComponentModel.DataAnnotations** namespace in the Blazor application.
19
+
2. Bind the DataGrid to a model using `TValue` and [DataSource](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.SfGrid-1.html#Syncfusion_Blazor_Grids_SfGrid_1_DataSource).
20
+
3. Apply annotation attributes to model properties to enforce validation and display rules during CRUD operations.
21
+
22
+
### Supported Data Annotation Attributes
23
+
24
+
The tables categorize supported attributes by display, formatting, metadata, and validation functionality.
25
+
26
+
### Display Attributes
27
+
28
+
Use **Display** attributes to control how column headers, ordering, and metadata appear in the grid interface.
29
+
30
+
| Attribute Name | Properties | Functionality |
31
+
|----------------|------------|---------------|
32
+
| Display | Name | Sets the header text for the DataGrid column |
33
+
| Display | ShortName | Sets a shorter version of the header text |
34
+
| Display | AutoGenerateField | Prevents the column from being auto-generated |
35
+
| Display | AutoGenerateFilter | Disables filtering for the column |
36
+
| Display | Description | Sets tooltip text shown on column ellipsis hover |
37
+
| Display | Order | Defines the display order of the column |
38
+
39
+
### DisplayFormat Attributes
40
+
41
+
Apply **DisplayFormat** attributes when column values require specific formatting or null-handling behavior.
19
42
20
43
| Attribute Name | Properties | Functionality |
21
-
|---------------|------------|--------------|
22
-
| Display | Name | Sets the header text for the Grid column |
23
-
| Display | ShortName | Sets a shorter version of the header text for the Grid column |
24
-
| Display | AutoGenerateField | Prevents the column from being auto-generated in the Grid |
25
-
| Display | AutoGenerateFilter | Specifies whether filtering should be disabled for the column |
26
-
| Display | Description | Sets the tooltip text displayed when hovering over the column ellipsis |
27
-
| Display | Order | Defines the display order priority of the Grid column |
28
-
| DisplayFormat | FormatString | Sets the format for displaying data in the column |
29
-
| DisplayFormat | ApplyFormatInEditMode | Determines whether the format should be applied during edit mode |
30
-
| DisplayFormat | NullDisplayText | Sets the text to be displayed when the value of the property is null |
31
-
| DisplayFormat | ConvertEmptyStringToNull | Determines whether empty string values should be converted to null in the user interface |
32
-
| DisplayFormat | NeedsHtmlEncode | Sets whether HTML encoding should be disabled for a particular column |
33
-
| ScaffoldColumnAttribute | Scaffold | Sets whether the column is visible in the user interface |
| Key | Key | Marks a column as the primary key in the Grid |
36
-
| Validation Attributes:<br><br>1. RequiredAttribute<br>2. StringLengthAttribute<br>3. RangeAttribute<br>4. RegularExpressionAttribute<br>5. MinLengthAttribute<br>6. MaxLengthAttribute<br>7. EmailAddressAttribute<br>8. CompareAttribute<br> || These validation attributes are used as `validation rules` in Grid CRUD operations |
37
-
38
-
> The Syncfusion<supstyle="font-size:70%">®</sup> Blazor DataGrid property takes precedence over data annotation attributes. For example, when both the DisplayName attribute and `HeaderText` are assigned to a field in the Grid model class for a specific column, the `HeaderText` value will be prioritized and displayed in the Grid header.
39
-
40
-
The following sample code demonstrates how to use data annotations in the Grid:
44
+
|----------------|------------|---------------|
45
+
| DisplayFormat | FormatString | Sets the display format for column data |
46
+
| DisplayFormat | ApplyFormatInEditMode | Applies format during edit mode |
47
+
| DisplayFormat | NullDisplayText | Displays custom text when the value is null |
48
+
| DisplayFormat | ConvertEmptyStringToNull | Converts empty strings to null in the UI |
49
+
| DisplayFormat | HtmlEncode | Enables or disables HTML encoding for display |
50
+
51
+
### Other Metadata Attributes
52
+
53
+
Use these attributes to manage column visibility, editing behavior, and key definitions.
54
+
55
+
| Attribute Name | Properties | Functionality |
56
+
|----------------|------------|---------------|
57
+
| ScaffoldColumnAttribute | Scaffold | Controls column visibility in the UI |
58
+
| ReadOnlyAttribute | IsReadOnly | Prevents editing of the column |
59
+
| Key | Key | Marks the column as the primary key |
60
+
61
+
### Validation Attributes
62
+
63
+
Add validation attributes to enforce rules that display inline Blazor DataGrid validation messages during CRUD operations.
64
+
65
+
- RequiredAttribute
66
+
- StringLengthAttribute
67
+
- RangeAttribute
68
+
- RegularExpressionAttribute
69
+
- MinLengthAttribute
70
+
- MaxLengthAttribute
71
+
- EmailAddressAttribute
72
+
- CompareAttribute
73
+
74
+
> When both the `Display` attribute’s `Name` and the column’s `HeaderText` property are defined, the `HeaderText` value takes precedence and is shown in the column header.
75
+
76
+
The `Display` attribute can be used to show user-friendly labels for enum values. This improves readability by replacing raw enum values with descriptive names.
> The **Verified** column displays the `Enum` member using the `Display` attribute name, enhancing user experience by rendering a human-readable label instead of the raw enum value.
199
+

160
200
161
-
> You can refer to our[Syncfusion<supstyle="font-size:70%">®</sup> Blazor DataGrid](https://www.syncfusion.com/blazor-components/blazor-datagrid) feature tour page for its features. You can also explore our [Syncfusion<supstyle="font-size:70%">®</sup> Blazor DataGrid example](https://blazor.syncfusion.com/demos/datagrid/overview?theme=bootstrap5) to understand how to present and manipulate data.
201
+
N> Refer to the[Syncfusion<supstyle="font-size:70%">®</sup> Blazor DataGrid](https://www.syncfusion.com/blazor-components/blazor-datagrid) feature tour for an overview of available features. Explore the [Blazor DataGrid example](https://blazor.syncfusion.com/demos/datagrid/overview?theme=bootstrap5) to see how data is presented and managed within an application.
0 commit comments