|
<SfGrid DataSource="@Orders" Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" })"
AllowSelection="true" AllowFiltering="true">
<GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true"></GridEditSettings>
<GridColumns>
. . .
<GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" ValidationRules="@(new ValidationRules{ Required= true })" TextAlign="TextAlign.Right" Width="120">
</GridColumn>
</GridColumns>
</SfGrid>
@code{
public List<Order> Orders { get; set; }
. ..
public class Order
{
public int? OrderID { get; set; }
public string CustomerID { get; set; }
public DateTime? OrderDate { get; set; }
[CustomValidation]
public double? Freight { get; set; }
public int? MaxQuantity { get; set; }
}
[AttributeUsage(AttributeTargets.Property)]
public class CustomValidationAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var Res = false;
if((double?)value != null)
{
if ((double)value > (validationContext.ObjectInstance as Order).MaxQuantity)
{
Res = true;
}
}
return Res ? new ValidationResult("Freight Value must be smaller", new[] { validationContext.MemberName }) : ValidationResult.Success; //if the entered value in Freight column exceeds the MaxQuantity value of the record, then the validation error will be shown
}
}
}
|
|
<SfGrid DataSource="@Orders" Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" })"
AllowSelection="true" AllowFiltering="true">
<GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true"></GridEditSettings>
<GridColumns>
. . .
<GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" ValidationRules="@(new ValidationRules{ Required= true })" TextAlign="TextAlign.Right" Width="120">
</GridColumn>
</GridColumns>
</SfGrid>
@code{
public List<Order> Orders { get; set; }
. ..
public class Order
{
public int? OrderID { get; set; }
public string CustomerID { get; set; }
public DateTime? OrderDate { get; set; }
[CustomValidation]
public double? Freight { get; set; }
public int? MaxQuantity { get; set; }
}
[AttributeUsage(AttributeTargets.Property)]
public class CustomValidationAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var Res = false;
if((double?)value != null)
{
if ((double)value > (validationContext.ObjectInstance as Order).MaxQuantity)
{
Res = true;
}
}
return Res ? new ValidationResult("Freight Value must be smaller", new[] { validationContext.MemberName }) : ValidationResult.Success; //if the entered value in Freight column exceeds the MaxQuantity value of the record, then the validation error will be shown
}
}
}
|