private void AddRecordsToGroup(
            UPMRepeatableEditGroup repeatableEditGroup,
            int count,
            UPCRMResult childResult,
            Dictionary <string, UPCRMRecord> changedRecords,
            FieldControlTab childFieldControlTab)
        {
            for (var i = 0; i < count; i++)
            {
                var childRow         = (UPCRMResultRow)childResult.ResultRowAtIndex(i);
                var childEditContext = new UPChildEditContext(childRow, this.NextPostfix);
                var changedRecord    = changedRecords.ValueOrDefault(childRow.RootRecordId);
                var initialRecords   = changedRecord != null ? new List <UPCRMRecord> {
                    changedRecord
                } : null;

                var editFieldContextArray = this.EditContextsForResultRow(childRow, childFieldControlTab, childEditContext.EditFieldContext, null, childEditContext.FieldLabelPostfix, initialRecords);
                var editFieldCount        = editFieldContextArray.Count;
                if (editFieldCount > 0)
                {
                    var standardGroup = new UPMStandardGroup(new RecordIdentifier(childRow.RootRecordIdentification))
                    {
                        Deletable = this.DeleteRecordEnabled
                    };
                    childEditContext.Group = standardGroup;
                    this.AddChildRecordContext(childEditContext);
                    for (var j = 0; j < editFieldCount; j++)
                    {
                        var field = editFieldContextArray[j];
                        AddFieldToGroup(standardGroup, field, childEditContext);
                    }

                    childEditContext.HandleDependentFields();
                    repeatableEditGroup.AddChild(standardGroup);
                }
            }
        }
        private UPMStandardGroup CreateNewGroupRecordAsNew(Dictionary <string, object> parentInitialValues, UPCRMRecord record, bool asNew)
        {
            UPContainerMetaInfo metaInfo = new UPContainerMetaInfo(this.ChildFieldControl);
            UPCRMResult         result   = !string.IsNullOrEmpty(record?.RecordId) ? metaInfo.NewRecordWithRecordId(record.RecordId) : metaInfo.NewRecord();

            UPCRMResultRow     childRow         = (UPCRMResultRow)result.ResultRowAtIndex(0);
            UPChildEditContext childEditContext = new UPChildEditContext(childRow, this.NextPostfix);

            if (asNew)
            {
                childEditContext.SetAsNew();
            }

            if (record != null)
            {
                childEditContext.AddChangedLinksFromRecordParentLink(record, null);
            }

            FieldControlTab childFieldControlTab = this.ChildFieldControl.TabAtIndex(0);

            this.combinedInitialValues = null;
            if (this.initialValues != null)
            {
                if (parentInitialValues != null)
                {
                    Dictionary <string, object> combined = new Dictionary <string, object>(this.initialValues);
                    foreach (var item in parentInitialValues)
                    {
                        combined[item.Key] = item.Value;
                    }

                    this.combinedInitialValues = combined;
                }
                else
                {
                    this.combinedInitialValues = this.initialValues;
                }
            }
            else
            {
                this.combinedInitialValues = parentInitialValues;
            }

            List <UPCRMRecord> initialRecords = record != null ? new List <UPCRMRecord> {
                record
            } : null;

            List <object> editFieldContextArray = this.EditContextsFor(childRow, childFieldControlTab,
                                                                       childEditContext.EditFieldContext, this.combinedInitialValues, initialRecords);
            int editFieldCount = editFieldContextArray.Count;

            if (editFieldCount > 0)
            {
                UPMStandardGroup group = new UPMStandardGroup(new RecordIdentifier(childRow.RootRecordIdentification));
                group.Deletable        = true;
                childEditContext.Group = group;
                this.AddChildRecordContext(childEditContext);
                for (int j = 0; j < editFieldCount; j++)
                {
                    UPEditFieldContext editFieldContext = editFieldContextArray[j] as UPEditFieldContext;
                    if (editFieldContext != null)
                    {
                        editFieldContext.ChildEditContext  = childEditContext;
                        editFieldContext.FieldLabelPostfix = childEditContext.FieldLabelPostfix;
                        List <UPMEditField> editFields = editFieldContext.EditFields;
                        if (editFields.Count > 0)
                        {
                            foreach (UPMEditField editField in editFields)
                            {
                                editField.EditFieldsContext = childEditContext;
                                group.AddField(editField);
                            }
                        }
                        else
                        {
                            UPMField field = editFieldContext.Field;
                            if (field != null)
                            {
                                group.AddField(field);
                            }
                        }
                    }
                    else
                    {
                        group.AddField((UPMField)editFieldContextArray[j]);
                    }
                }

                childEditContext.HandleDependentFields();
                return(group);
            }

            return(null);
        }