Exemplo n.º 1
0
        /// <summary>
        /// 绑定实体数据
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <param name="isCopy">是否复制</param>
        public virtual void BindMethod(E entity, bool isCopy = false)
        {
            IsInnerSetValue = true;
            CurrentEntity   = entity;
            BindEventArgs args = new BindEventArgs();

            args.IsNew      = IsNew;
            args.IsCopy     = isCopy;
            args.BindEntity = entity;
            OnBind(args);
            if (args.IsSuccess)
            {
                if (IsNew && Metadata != null && Grid != null && Metadata.ParentFieldName.IsNotEmpty())
                {
                    int parentID = 0;
                    if (ParentEntity != null)
                    {
                        parentID = ParentEntity.ID;
                    }
                    CurrentEntity.SetPropertyValue(Metadata.ParentFieldName, parentID);
                }

                if (EditPanel != null)
                {
                    FormHelper.BindControlValue(EditPanel, entity);
                }

                if (StatusPanel != null)
                {
                    FormHelper.BindControlValue(StatusPanel, entity);
                }
                SetButtonStatus();
            }
            IsInnerSetValue = false;
        }
Exemplo n.º 2
0
        /// <summary>
        /// 保存数据具体实现
        /// </summary>
        /// <returns>成功返回True</returns>
        protected bool SaveCore()
        {
            try
            {
                if (!ValidateControl(null))
                {
                    return(false);
                }
                SaveEventArgs beforeargs = new SaveEventArgs();
                beforeargs.IsNew      = IsNew;
                beforeargs.BindEntity = CurrentEntity;
                OnBeforeSave(beforeargs);
                if (!beforeargs.IsSuccess)
                {
                    return(false);
                }

                if (CurrentEntity == null)
                {
                    return(false);
                }

                if (IsNew)
                {
                    int sortCode = 0;
                    if (ParentNode == null && Grid.Nodes.Count > 0)
                    {
                        sortCode = Grid.Nodes.Count - 1;
                    }
                    else if (ParentNode != null)
                    {
                        var pNode = Grid.FindNodeByID(ParentNode.Id);
                        if (pNode != null)
                        {
                            sortCode = pNode.Nodes.Count;
                        }
                    }
                    CurrentEntity.SetPropertyValue(Metadata.SortCodeFieldName, sortCode);
                    Service.Create(CurrentEntity);
                    if (Grid.DataSource != null)
                    {
                        if (Grid.DataSource is IList)
                        {
                            Grid.Add(CurrentEntity);
                        }
                        else if (Grid.DataSource is DataTable)
                        {
                            var row = Factory.Default.MapToDataRow(Grid.TableSource, CurrentEntity);
                            Grid.Add(row);
                        }
                    }
                }
                else
                {
                    Service.Update(CurrentEntity);
                    if (Grid.DataSource is IList)
                    {
                        Grid.Update(CurrentEntity);
                    }
                    else if (Grid.DataSource is DataTable)
                    {
                        var index = Grid.TableSource.Rows.IndexOf(Grid.GetSelected <DataRow>());
                        if (index > -1)
                        {
                            var row = Grid.TableSource.Rows[index];
                            Factory.Default.UpdateDataRow(row, CurrentEntity);
                        }
                    }
                }

                SaveEventArgs afterArgs = new SaveEventArgs();
                afterArgs.IsNew      = IsNew;
                afterArgs.BindEntity = CurrentEntity;
                OnAfterSave(afterArgs);
                if (afterArgs.IsSuccess)
                {
                    SetButtonStatus();
                }
                IsNew = false;
                IsEditValueChanged = false;
                return(true);
            }
            catch (VersionException ex)
            {
                //LogManager.Error(ex.Message, "AppUser", ex);
                var newEntity = (E)ex.NewEntity;
                BindMethod(newEntity);
                //Grid.UpdateEntity(newEntity);
                XtraMessageBoxHelper.ShowError(ex.Message);
            }
            catch (Exception ex)
            {
                XtraMessageBoxHelper.ShowError(ex.Message);
                //LogManager.Error(ex.Message, "AppUser", ex);
            }
            return(false);
        }