This is a migrated thread and some comments may be shown as answers.

Force Validation On New Record Insert

16 Answers 590 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Mark asked on 15 Jul 2010, 08:40 PM
In my gridview, I only allow unique combination of values in three of my columns.  However, I want to allow the user to "clone" an existing row, and change the values in the other columns (those not constrained).  How can I copy a new row, and initially show the new row to be in an invalid state?  When I copy a new row now, it appears to be just another, valid row.

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

Sort by
0
Nedyalko Nikolov
Telerik team
answered on 19 Jul 2010, 02:19 PM
Hi Mark,

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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Mark
Top achievements
Rank 1
answered on 19 Jul 2010, 04:14 PM
Thank you for the sample code, however, I do not see how this is an example of how to initially show a new row (or copied row) to be in an invalid state.  This code only fires after you've finished with an edit.  I can do (and have done) that now.

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
0
Milan
Telerik team
answered on 22 Jul 2010, 05:28 AM
Hello 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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Mark
Top achievements
Rank 1
answered on 22 Jul 2010, 07:46 PM
Thank you, but would you happen to have an example of this?  When I search for "GetError", nothing is found.  If you meant "GetErrors" then that only returns one result:

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
0
Milan
Telerik team
answered on 23 Jul 2010, 11:20 AM
Hi 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.

As you will see the last record item in the sample is created will null FirstName which will be picked up by the grid.


Best wishes,
Milan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Mark
Top achievements
Rank 1
answered on 23 Jul 2010, 03:34 PM
Thank you for the example.  That is a good and potentially useful one, however... In my gridview, I only allow unique combination of values in three of my columns.  I need to validate the entire row, not just one column.

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.
0
Milan
Telerik team
answered on 29 Jul 2010, 10:31 AM
Hello Mark,

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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Combinations
Top achievements
Rank 1
answered on 08 May 2012, 01:46 PM
I have a similar problem as Mark but my problem is that I want to validate rows after multiple rows have been pasted.

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);
}
... but then only the last item is shown as red in the grid, even though all my pasted items were invalid.

Is there a way to validate multiple rows programmatically?
0
Nedyalko Nikolov
Telerik team
answered on 09 May 2012, 07:28 AM
Hello,

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 >>

0
Combinations
Top achievements
Rank 1
answered on 04 Jul 2012, 06:39 AM
Hi,
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)?
0
Nedyalko Nikolov
Telerik team
answered on 06 Jul 2012, 06:10 AM
Hi,

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.

All the best,
Nedyalko Nikolov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Vic
Top achievements
Rank 1
answered on 06 Jul 2012, 01:56 PM
Nedyalko,
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?
0
Nedyalko Nikolov
Telerik team
answered on 11 Jul 2012, 06:02 AM
Hi,

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.

Regards,
Nedyalko Nikolov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Vic
Top achievements
Rank 1
answered on 17 Jul 2012, 11:31 PM
Nedyalko,

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;
             
        }
    }
0
Nedyalko Nikolov
Telerik team
answered on 23 Jul 2012, 07:07 AM
Hi,

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.

0
Vic
Top achievements
Rank 1
answered on 23 Jul 2012, 09:52 PM
Nedyalko,
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!
Tags
GridView
Asked by
Mark
Top achievements
Rank 1
Answers by
Nedyalko Nikolov
Telerik team
Mark
Top achievements
Rank 1
Milan
Telerik team
Combinations
Top achievements
Rank 1
Vic
Top achievements
Rank 1
Share this question
or