8 Answers, 1 is accepted
This is a built-in feature of RadGridView. I've just tested it and everything work as expected.
Just add a property to your business object something like following:
[Range(5, 10)] public int Age { get { return this.age; } set { if (this.age != value) { this.age = value; this.OnPropertyChanged("Age"); } } }And validation should be shown on a row level. You can take a look at the attached image. All the best,
Nedyalko Nikolov
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
If the property get changed outside the gridview and the value fail validation, the grid view does not show the error until the row get edited within the gridview.
All you have to do is to implement INotifyPropertyChanged interface for your business object.
I've just tested and everything works as expected.
Let me know if this does not help.
Nedyalko Nikolov
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
public class City : INotifyPropertyChanged { private int id; private string name; private ObservableCollection<City> cityList; private bool isDeleted; [IsUniqueAttribute] public int Id { get { return this.id; } set { this.id = value; OnPropertyChanged("Id"); } } [Required] public string Name { get { return this.name; } set { this.name = value; OnPropertyChanged("Name"); } } public ObservableCollection<City> CityList { get { return this.cityList; } set { this.cityList = value; OnPropertyChanged("CityList"); } } [MustBeFalseAttribute] public bool IsDeleted { get { return this.isDeleted; } set { this.isDeleted = value; OnPropertyChanged("IsDeleted"); } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(PropertyChangedEventArgs args) { PropertyChangedEventHandler handler = this.PropertyChanged; if (handler != null) { handler(this, args); } } private void OnPropertyChanged(string propertyName) { this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } public class IsUniqueAttribute : ValidationAttribute { protected override ValidationResult IsValid(object value, ValidationContext validationContext) { City instance = validationContext.ObjectInstance as City; if (instance != null) { bool exists = instance.CityList.Any(c => c.Id == instance.Id && !c.Equals(instance)); if (exists) { return new ValidationResult("Not unique"); } else { return ValidationResult.Success; } } return base.IsValid(value, validationContext); } } public class MustBeFalseAttribute : ValidationAttribute { protected override ValidationResult IsValid(object value, ValidationContext validationContext) { City instance = validationContext.ObjectInstance as City; if (instance != null) { if (instance.IsDeleted) { return new ValidationResult("Must be False"); } else { return ValidationResult.Success; } } return base.IsValid(value, validationContext); } } }My ViewModel also does.
public class MyViewModel : INotifyPropertyChanged { private ObservableCollection<City> cities; public MyViewModel() { this.Cities = new ObservableCollection<City>(); this.Cities.Add(new City { Id = 1, Name = "Chicago", CityList = this.Cities }); this.Cities.Add(new City { Id = 2, Name = "New York", CityList = this.Cities }); this.Cities.Add(new City { Id = 2, Name = "Miami", CityList = this.Cities }); this.Cities.Add(new City { Id = 3, Name = "San Francisco", CityList = this.Cities, IsDeleted = true }); //this.FetchDcfViewModels(); } public ObservableCollection<City> Cities { get { return this.cities; } set { this.cities = value; OnPropertyChanged("Cities"); } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(PropertyChangedEventArgs args) { PropertyChangedEventHandler handler = this.PropertyChanged; if (handler != null) { handler(this, args); } } private void OnPropertyChanged(string propertyName) { this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } }The error shows for "San Francisco" at load.
When I set "Chicago" to IsDeleted to True in code. The row does not show the error until I edit the city name for the "Chicago" row directly in the gridview.
private void Button1_Click(object sender, RoutedEventArgs e){ ((MyViewModel)this.DataContext).Cities[0].IsDeleted = true;}I'm attaching a modified version of the example from previous posts that demonstrates how to meet your goals.
Let me know how it works on your end.
Nedyalko Nikolov
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
Follow up:
I've forgotten to give you instructions how to use the example.
1. Hit "F5".
2. Press "Do something 1" button.
3. Press "Do something 2" button.
Nedyalko Nikolov
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.