Skip to content

Commit 02b2689

Browse files
Merge pull request #3910 from syncfusion-content/datavalidation
Prepared UG for the data validation annotation support.
2 parents 3d86093 + 0189d2c commit 02b2689

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

MAUI/DataGrid/data-validation.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,74 @@ Enable built-in validation support by setting [SfDataGrid.ValidationMode](https:
156156

157157
<img alt="data-validation-with-INotifyDataErrorInfo" src="Images\data-validation\maui-datagrid-datavalidation-inotifydataerrorinfo.png" width="464" height="396"/>
158158

159+
## Built-in validation using Data Annotation
160+
161+
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+
159227
## Cell Validation
160228

161229
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

Comments
 (0)