Note: The RowValidating event only seems to fire when I've completed an edit on that row. I need something similar, but outside the scope of editing.
16 Answers, 1 is accepted
Indeed RowValidating event fires only when edit is about to be committed.
You can take a look at this blog post how to achieve asynchronous validation which you could use in your scenario. Also I'm attaching an example for WPF.
All the best,
Nedyalko Nikolov
the Telerik team
How can I copy a new row, and initially show the new row to be in an invalid state - without going into edit mode first?
Thank you,
Mark
Whenever a row is loaded the grid will call the GetError method for all of its visible properties and if some of them are invalid this will be reflected in the UI state of the row and its cells. To relate that to your case the grid will call GetError for all properties of your new row and if GetError returns some errors they will be displayed on the UI. For example, if the GetError method you can return some kind of error if FirstName is null or empty string.
All the best,
Milan
the Telerik team
http://www.telerik.com/help/wpf/radgridview-howto-create-custom-editor.html
And once again here... in that example... we're in edit mode. I need to validate the row as it's loading (or loaded).
Mark
The validation can happen on the data level - i.e. on the data item themselves. You can find more information about the IDataErrorInfo on this blog post.
I have updated the original application of Nedyalko and now all of the validation is done in the "
public string this[string columnName]" method of Person.Best wishes,
As you will see the last record item in the sample is created will null FirstName which will be picked up by the grid.
Milan
the Telerik team
Attached is a screen capture of what I'm talking about... In this example, no matter the content of the "Value" column, the entire row is invalid because the "Application", "Environment", and "Machine" columns are the same as the previous row.
This was achieved after an edit, but I need this after a copy or load of the grid.
You can try the following: Once a new record is cloned and inserted you can force it to enter edit mode and right after that try to commit the edit. That way the logic in RowValidating will be executed.
I have attached the updated sample project.
Sincerely yours,
Milan
the Telerik team
I tried to use the code from the sample and do something like this after a paste action:
foreach
(var x
in
_pastedItems)
{
this
.Dispatcher.BeginInvoke(
new
Action(() =>
this
.ValidateNewPerson(x)),
System.Windows.Threading.DispatcherPriority.ApplicationIdle,
null
);
}
Is there a way to validate multiple rows programmatically?
We've almost finished an improvement of RadGridView's validation logic, and such scenarios will be handled out of the box. RadGridView will show the invalid rows accordingly (with an error sign and a tooltip) (of course only for items in view). So stay tuned and update to our latest version when validation improvement will be ready. I expect that we will manage to include it in a couple of weeks.
Greetings,Nedyalko Nikolov
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
Is there any validation improvements in the 2012 Q2 release?
If so, where can I learn about how to use the GridView to validate multiple pasted rows (as asked in my previous question)?
Improvements in validation are available with the latest official version. All you have to do is to choose a validation method that covers your needs:
1. IDataErrorInfo.
2. Telerik.Windows.Data.INotifyDataErrorInfo.
3. Binding validation (via exceptions in setters).
4. DataAnnotation attributes.
I personally prefer methods 2 and 4, since you will have the ability to perform validation on both row and cell level.
Nedyalko Nikolov
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
I have followed the guidance in your documentation (http://www.telerik.com/help/wpf/gridview-managing-data-validation.html) to implement validation using either CellValidating event or Data Annotations and it is not working when copy/pasting rows into the grid.
Can you post or point to a clear example showing successful validation when pasting rows?
I'm attaching a sample application that demonstrates how to validate objects with RadGridView using DataAnnotation attributes.
1. Start the application.
2. Open Book1.xls within project directory and copy the content into clipboard.
3. Select the last row of RadGridView and press Ctrl + V.
Now you should see the error inside Age cell - Person.Age property has Range attribute that allows numbers from 0 to 100.
Nedyalko Nikolov
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
Thank you for posting the sample app. I'm in the process of analyzing my app to determine why it will not work in my case (I'm not using MVVM).
In the meantime, though, I modified your app to test other validation attributes such as Required, StringLength, and a custom one that checks for unique values somewhat similar to my app's requirements. I must be missing something, as I can't get them to work. I'm attaching the modified app and would appreciate your having a look. If you'd like me to create a new thread or submit a support ticket, I'd be happy to. Thanks very much for your help..
[EDIT: Unable to attach zip file so am copy-pasting the modified code below]
public
class
Person : INotifyPropertyChanged
{
#region INotifyPropertyChanged Members
public
event
PropertyChangedEventHandler PropertyChanged;
private
void
OnPropertyChanged(
string
propertyName)
{
if
(
this
.PropertyChanged !=
null
)
this
.PropertyChanged(
this
,
new
PropertyChangedEventArgs(propertyName));
}
#endregion
[StringLength(4)]
[Required(AllowEmptyStrings=
false
, ErrorMessage=
"FirstName is required"
)]
private
string
firstName;
public
string
FirstName
{
get
{
return
this
.firstName;
}
set
{
if
(
this
.firstName != value)
{
this
.firstName = value;
this
.OnPropertyChanged(
"FirstName"
);
}
}
}
[StringLength(4)]
[Required(AllowEmptyStrings =
false
, ErrorMessage =
"LastName is required"
)]
[Unique(ErrorMessage =
"That LastName already exists"
)]
private
string
lastName;
public
string
LastName
{
get
{
return
this
.lastName;
}
set
{
if
(
this
.lastName != value)
{
this
.lastName = value;
this
.OnPropertyChanged(
"LastName"
);
}
}
}
private
int
age;
[Range(0, 100)]
public
int
Age
{
get
{
return
this
.age;
}
set
{
if
(
this
.age != value)
{
this
.age = value;
this
.OnPropertyChanged(
"Age"
);
}
}
}
private
int
genderID;
public
int
GenderID
{
get
{
return
this
.genderID;
}
set
{
if
(
this
.genderID != value)
{
this
.genderID = value;
this
.OnPropertyChanged(
"GenderID"
);
}
}
}
}
class
UniqueAttribute : ValidationAttribute
{
public
override
bool
IsValid(
object
value)
{
// Unfamiliar with proper use of view model... but the idea is to ensure a unique value in the collection
ViewModel viewModel =
new
ViewModel();
return
viewModel.Persons.Where(psn => psn.LastName == value.ToString()).Count() == 0;
}
}
Generally you should validate property value with one and the same instance of the view model class. Such unique checks are called against some database, so this should not be a problem. I'm attaching a modified version of my example which uses singleton design pattern in order to ensure one and the same instance of the viewmodel. Unfortunately there is no way to create it in xaml, therefore I've created DataContext in code behind.
All the best,Nedyalko Nikolov
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
I really appreciate your help and the sample project using the singleton pattern.
The reason the other validation attributes (Required, StringLength) weren't working for me is that I was applying them to the private field member instead of the public property (silly mistake).
Thanks again!