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

10 posts, 0 answers
  1. Scott Waye
    Scott Waye avatar
    111 posts
    Member since:
    Nov 2009

    Posted 23 Oct 2014 Link to this post

    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,
  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 27 Oct 2014 Link to this post

    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.

     
  3. DevCraft banner
  4. Scott Waye
    Scott Waye avatar
    111 posts
    Member since:
    Nov 2009

    Posted 27 Oct 2014 in reply to Maya Link to this post

    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?
  5. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 28 Oct 2014 Link to this post

    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.

     
  6. Scott Waye
    Scott Waye avatar
    111 posts
    Member since:
    Nov 2009

    Posted 28 Oct 2014 in reply to Maya Link to this post

    Hi, That doesn't really work as the addingnewdataitem event is fired before the cellvalidating.
  7. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 29 Oct 2014 Link to this post

    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.

     
  8. Scott Waye
    Scott Waye avatar
    111 posts
    Member since:
    Nov 2009

    Posted 29 Oct 2014 in reply to Maya Link to this post

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

  9. Scott Waye
    Scott Waye avatar
    111 posts
    Member since:
    Nov 2009

    Posted 29 Oct 2014 in reply to Scott Waye Link to this post

    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.
  10. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 30 Oct 2014 Link to this post

    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.

     
  11. Scott Waye
    Scott Waye avatar
    111 posts
    Member since:
    Nov 2009

    Posted 30 Oct 2014 in reply to Maya Link to this post

    Yes thanks, I think we are ok.
Back to Top
DevCraft banner