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
You can validate the data using **data annotation attributes** by setting
162
+
[SfDataGrid.ValidationMode](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.DataGrid.SfDataGrid.html#Syncfusion_Maui_DataGrid_SfDataGrid_ValidationMode) or [DataGridColumn.ValidationMode](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.DataGrid.DataGridColumn.html#Syncfusion_Maui_DataGrid_DataGridColumn_ValidationMode) property to `InEdit` or `InView`.
163
+
164
+
### Using different annotations
165
+
166
+
The numeric type like int, double, decimal properties can be validated using [Range attributes](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.rangeattribute?view=net-5.0).
167
+
168
+
{% tabs %}
169
+
{% highlight c# %}
170
+
private int orderID;
171
+
172
+
[Range(1001, 1005, ErrorMessage = "OrderID between 1001 and 1005 alone processed")]
173
+
public int OrderID
174
+
{
175
+
get { return orderID; }
176
+
set { orderID = value; }
177
+
}
178
+
179
+
private decimal price;
180
+
181
+
[Range(typeof(decimal),"12","20")]
182
+
public decimal Price
183
+
{
184
+
get { return price; }
185
+
set { price = value; }
186
+
}
187
+
{% endhighlight %}
188
+
{% endtabs %}
189
+
190
+
The string type property can be validated using [Required](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.requiredattribute?view=net-5.0), [String Length attributes](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.stringlengthattribute?view=net-5.0)
191
+
192
+
{% tabs %}
193
+
{% highlight c# %}
194
+
private string shippingCity;
195
+
196
+
[Required]
197
+
public string ShipCity
198
+
{
199
+
get { return shippingCity; }
200
+
set { shippingCity = value; }
201
+
}
202
+
203
+
private string customerName;
204
+
205
+
[StringLength(17)]
206
+
public string CustomerName
207
+
{
208
+
get { return customerName; }
209
+
set { customerName = value; }
210
+
}
211
+
{% endhighlight %}
212
+
{% endtabs %}
213
+
214
+
The data that has heterogeneous type (combination of number, special character) can be validated using [RegularExpressions](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.regularexpressionattribute?view=net-5.0).
215
+
216
+
{% tabs %}
217
+
{% highlight c# %}
218
+
[RegularExpressionAttribute(@"^[a-zA-Z]{1,40}$", ErrorMessage="Numbers and special characters not allowed")]
219
+
public string CustomerID
220
+
{
221
+
get { return customerId; }
222
+
set { customerId = value; }
223
+
}
224
+
{% endhighlight %}
225
+
{% endtabs %}
226
+
159
227
## Cell Validation
160
228
161
229
A cell can be validated using [CellValidating](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.DataGrid.SfDataGrid.html#Syncfusion_Maui_DataGrid_SfDataGrid_CellValidating) event when the cell is edited. `CellValidating` event occurs when the edited cells tries to commit the data or lose the focus. DataGrid will not allow user to edit other cells if validation failed.
0 commit comments