Set default values in Editable Child Collection bound to DataGridView

Set default values in Editable Child Collection bound to DataGridView

Old forum URL: forums.lhotka.net/forums/t/11487.aspx


superkuton posted on Saturday, July 21, 2012

I have read several blog posts and the e-books but I cant really implement it properly.

What are the basics of setting default property values of an object in an Editable Child Collection, which is bound to a DataGridView?

 

RockyRocks replied on Saturday, July 21, 2012

Your collection will have an AddNewCore method which will call a factory method on your child object when a new instance is added to the list.

That factory method makes the addition of an object in this case work the normal way; there is code in your child that gets called when a new object is created (called by the child data portal) and that is where you do your defaulting. Depending on your version of CSLA and data access decisions it could be in Child_Create method.

JonnyBee replied on Saturday, July 21, 2012

I typically define default values on the static PropertyInfo and then set default values in Child_Create method.

When defautvalues is defined in PropertyInfo´s - the default value vill be set in the first Property Get method on a "managed" field. 
For private backing fields you must set the default value - preferrably in Child_Create or use private field initializer. 

superkuton replied on Saturday, July 21, 2012

Thank you RockyRocks and JonnyBee.

Kindly take a look on my code and see what I might be missing:

In my Editable Root Object:

        public static readonly PropertyInfo<PayableDetail> PayableDetailsProperty = RegisterProperty<PayableDetail>(c => c.PayableDetails);
        public PayableDetail PayableDetails
        {
            get
            {
                if (!(FieldManager.FieldExists(PayableDetailsProperty)))
                    LoadProperty(PayableDetailsProperty, DataPortal.CreateChild<PayableDetail>());
                return GetProperty(PayableDetailsProperty);
            }
            private set { LoadProperty(PayableDetailsProperty, value); }
        }

In my Editable Child Collection:

         public static PayableDetail NewPayableEditDetail()
        {
            return DataPortal.CreateChild<PayableDetail>();
        }

And in my Editable Child Object:

        [RunLocal]
        protected void Child_Create()
        {
            LoadProperty<int>(PayableIDProperty, 1);
            LoadProperty<string>(ProjectProperty, Budgeting.ProjectLookUp.DefaultValue());
            LoadProperty<int>(AccountIDProperty, 101);
       
            BusinessRules.CheckRules();
           
        }

 

Please advise. Thank you.

JonnyBee replied on Sunday, July 22, 2012

I don't understand your comment on "In my Editable Child Collection" - it seems to me like your code is a root object that uses a lazy loaded editable child. I do not see a collection beeing used here at all!!!

I´d first check what the values of PaymentDetail actually is. It could be your databing is not correct or not updating correctly. 

Possible resons and tings to check out:

1. PayableDetail field may alreade be set. (ie FieldManager.Exists returns true). I field is set to null or anyt value then FieldManager.Exists will return true).

2. AuthorizationRule on PayableDetails property may not allow property get (in which case get  retun null instead) 

3. PayableDetail object may have authorization rules thet does not allow user to read values (in which case the default value for datatype is returned).

3. I assume  the properties in PayableDetail are managed properties (no private backing fields).

4. I prefer to have lazy loading of properties like this (without property set method):

public static readonly PropertyInfo<PayableDetail> PayableDetailsProperty = RegisterProperty<PayableDetail>(c => c.PayableDetails);
public PayableDetail PayableDetails
{
   get
   {
        if (!(FieldManager.FieldExists(PayableDetailsProperty)))
             SetProperty(PayableDetailsProperty, PayableDetail.NewPayableEditDetail());
        return GetProperty(PayableDetailsProperty);
    }
}

Copyright (c) Marimer LLC