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

[Solved] Validate all rows in a grid

1 Answer 147 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Guy
Top achievements
Rank 1
Guy asked on 08 Sep 2011, 05:01 PM

Hi,

We have an editable grid. The object that are bound to the grid use DataAnnotation attribues for validation. When we are binding to an existing collection of objects some are invalid according to the validation rules (for example int property1 > int property2). If the user never makes any changes to these invalid rows the validation is never fired.

What we are trying to achieve is when the user clicks “Save” all rows in the grid are validated or they are validated one by one and the focus stops on the first invalid row i.e. save is prevented until all invalid rows are corrected.

Thanks

1 Answer, 1 is accepted

Sort by
0
Nedyalko Nikolov
Telerik team
answered on 15 Sep 2011, 09:02 AM
Hi Guy,

Sorry for the late answer.
There is no such built-in feature of RadGridView control, but you can achieve this with a few line of code. I'm pasting my code snippet:

private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            foreach (var item in this.radGridView.Items)
            {
                List<System.ComponentModel.DataAnnotations.ValidationResult> validationResults = new List<System.ComponentModel.DataAnnotations.ValidationResult>();
                var isValid = Validator.TryValidateObject(item, new ValidationContext(item, null, null), validationResults, true);
                if (!isValid)
                {
                    this.radGridView.CurrentColumn = this.radGridView.Columns.OfType<GridViewBoundColumnBase>().Where(c => c.GetDataMemberName() == validationResults[0].MemberNames.ElementAt(0)).First();
                    this.radGridView.ScrollIntoViewAsync(item
                        , this.radGridView.CurrentColumn
                        , (f) =>
                        {
                            GridViewRow row = f as GridViewRow;
                            if (row != null)
                            {
                                row.BeginEdit();
                                Dispatcher.BeginInvoke(() => this.radGridView.CommitEdit());
                            }
                        });
                    break;
                }
            }
        }

I'll try to clarify all steps. First of all you need to validate the object via Validator.TryValidateObject() method, this will reevaluate all DataAnnotation attributes and will return false if there is something wrong. The next step is to scroll to the item and put it in edit mode Validator.TryValidateObject also will return some information about the "invalid" property name, which could be used to locate the column bound to that property. Then all you have to do is to call BeginEdit() and grid will put the first "incorrect" cell in edit mode. Unfortunately this will not paint the row in "red". In order to mark this row in "red", you should call RadGridView.CommitEdit() via dispatcher, because GridViewRow should be in edit mode before CommitEdit call.
Of course you could skip CommitEdit() call, then GridViewRow will be in edit mode, but will not be marked as red.

Let me know if there is something unclear.

Regards,
Nedyalko Nikolov
the Telerik team

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

Tags
GridView
Asked by
Guy
Top achievements
Rank 1
Answers by
Nedyalko Nikolov
Telerik team
Share this question
or