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

Prevent new row in parent when new row in child has valiadtion errors

9 Answers 67 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Scott Waye
Top achievements
Rank 2
Veteran
Iron
Scott Waye asked on 23 Oct 2014, 01:15 PM
Hi,

Using 2014.2.729 I have a RadGridView with a child RadGridView through a HierarchyChildTemplate .  The first column in each grid is a combo box bound to a [Required] property in the business object, (validation attribute).  If a new child row is started, and then immediately a new parent row is started, then the combo boxs in the first columns of these 2 new rows go a bit strange and when clicked, they popup and immediate close as it seems the focus is trying to shift to the invalid row, but of course now there are 2 invalid rows, one in the parent grid and one in the child grid.   One solution would seem to be to cancel the new row add event in the parent if there is a child row which has an invalid row, either in the new row, or any other row, but how can I do that?

Thanks,

9 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 27 Oct 2014, 02:35 PM
Hi Scott,

Actually, such behavior is a kind of an expected one. Once the two grids go into invalid mode, both of them will try to steal the focus. Unfortunately, there is nothing we can do since we cannot predict whether there are more than one instances of RadGridView in the same focus field. In such scenario, you are the one to handle the case appropriately and ensure that if the first grid is invalid, the second one cannot begin editing.


Regards,
Maya
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Scott Waye
Top achievements
Rank 2
Veteran
Iron
answered on 27 Oct 2014, 03:53 PM
Hi,  That's fine, but how do I do that?  How can I test from the add new event, whether other grids are in an invalid state?
0
Maya
Telerik team
answered on 28 Oct 2014, 03:21 PM
Hello Scott,

What you can try is to handle CellValidating/CellValidated event for example and if it fails, cancel AddingNewDataItem event of the other instance of RadGridView. A global variable might be helpful in this case. 


Regards,
Maya
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Scott Waye
Top achievements
Rank 2
Veteran
Iron
answered on 28 Oct 2014, 09:26 PM
Hi, That doesn't really work as the addingnewdataitem event is fired before the cellvalidating.
0
Maya
Telerik team
answered on 29 Oct 2014, 09:45 AM
Hi Scott,

When there is RadGridView with an invalid row (with ActionOnLostFocus.CommitEdit and ValidatesOnDataErrors.Default), the user cannot interact with any other control since the GridView is bringing the focus to the invalid cell. The case becomes more complicated when there are two GridViews with the same settings. Clicking on the second GridView new row when there is already an invalid row in the first GridView, will result in both GridViews fighting for the focus and trying to get it back to their invalid cell. All operations happen asynchronously, thus making first GridView's RowValidating event to fire after second GridView's AddingNewDataItem event. This is why RadGridView cannot handle correctly your scenario.
In order to avoid the issue, you have the following three options:
  1. You can set ActionOnLostFocus to None or CancelEdit.
  2. You can subscribe to both GridView’s  AddingNewDataItem event and check whether the other one has any invalid rows. This approach would be hardly maintainable since you will need quite a few Boolean properties for different conditions.
  3. Another approach would be to set ValidatesOnDataErrors property to InViewMode. This will force the GridView to perform validation only in view mode (just after cell exits edit mode or initial data is loaded) and the user will be able to see the invalid rows and edit the other GridView at the same time.
 


Regards,
Maya
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Scott Waye
Top achievements
Rank 2
Veteran
Iron
answered on 29 Oct 2014, 01:28 PM
Hi, Thanks for the reply.

1 and 3 are not appealing for me due to lack of user feedback.  With 2 and a combo box bound to an int, the row does not leave edit mode so you never see the validation errors.

For 2, I think something like the below service is going to be ok.  You attach it to the grids in question, then when one of them tries to add a new row, the others have their commitedit called, if it returns false, then the add is cancelled.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
 
namespace doublegridaddvalidation
{
    public class AddRowWhenValidService
    {
        List<RadGridView> grids = new List<RadGridView>();
 
        public static AddRowWhenValidService GetService(RadGridView obj)
        {
            return (AddRowWhenValidService)obj.GetValue(ServiceProperty);
        }
 
        public static void SetService(DependencyObject obj, AddRowWhenValidService button)
        {
            obj.SetValue(ServiceProperty, button);
        }
 
        public static DependencyProperty ServiceProperty =
      DependencyProperty.RegisterAttached("ServiceProperty",
                                          typeof(AddRowWhenValidService),
                                          typeof(AddRowWhenValidService),
                                          new PropertyMetadata(null, ServiceChanged));
 
        static void ServiceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if(e.OldValue != null) throw new InvalidOperationException("AddRowWhenValidService cannot be changed");
            var grid = d as RadGridView;
            var service = e.NewValue as AddRowWhenValidService;
            if(grid != null && service != null)
            {
                service.AddGrid(grid);
            }
        }
 
        void AddGrid(RadGridView grid)
        {
            grids.Add(grid);   
            grid.AddingNewDataItem += GridOnAddingNewDataItem;
        }
 
        void GridOnAddingNewDataItem(object sender, GridViewAddingNewEventArgs args)
        {
            foreach(var grid in grids.Where(g => g.CanUserInsertRows && g != sender))
            {
                if (!grid.CommitEdit())
                {
                    Debug.WriteLine("commitEdit fales");
                    args.Cancel = true;
                    return;
                }
            }
        }
    }
}

0
Scott Waye
Top achievements
Rank 2
Veteran
Iron
answered on 29 Oct 2014, 01:29 PM
Sorry, that first sentence should read:

1 and 3 are not appealing for me due to lack of user feedback.  With 3
and a combo box bound to an int, the row does not leave edit mode so you
never see the validation errors.
0
Maya
Telerik team
answered on 30 Oct 2014, 02:53 PM
Hello Scott,


Based on your replies, I get the impression that you managed to find a solution for your application. Is that so or you need further assistance ?


Regards,
Maya
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Scott Waye
Top achievements
Rank 2
Veteran
Iron
answered on 30 Oct 2014, 03:01 PM
Yes thanks, I think we are ok.
Tags
GridView
Asked by
Scott Waye
Top achievements
Rank 2
Veteran
Iron
Answers by
Maya
Telerik team
Scott Waye
Top achievements
Rank 2
Veteran
Iron
Share this question
or