private void btnClose_Click(object sender, EventArgs e)
        {
            var eventArgs = new KPFormEventArgs();
            if (KPEventFormClosing != null)
            {
                eventArgs.Entity = this.DataSourceAltered;
                KPEventFormClosing(this, eventArgs);
            }

            if (!eventArgs.Cancel)
                ClosePage();
        }
        public bool SaveEntity()
        {
            try
            {
                // Reset DataSource
                DataSourceAltered = CloneDataSource(DataSource, TypeEntity);

                //Pega os dados da tela e preenche o DataSource
                RefreshDataSourceAltered();

                if (DataSourceAltered != null)
                {
                    if (Validate(ValidateEntity()))
                    {
                        if (KPEventBeforeSaveClick != null)
                        {
                            KPButtonEventsArgs eventArgs = new KPButtonEventsArgs();
                            KPEventBeforeSaveClick(DataSourceAltered, eventArgs);
                            if (eventArgs.Cancel)
                                return false;
                        }

                        ConstructorInfo entityBO = TypBOEntity.GetConstructor(new[] { TypeEntity });
                        if (entityBO == null)
                        {
                            throw new Exception(String.Format("Could not find a valid Constructor to this Entity ({0})", TypeEntity.FullName));
                        }
                        object entityBOInstance = entityBO.Invoke(new object[] { DataSourceAltered });

                        MethodInfo methodValidate = TypBOEntity.GetMethod("Validate");
                        if (methodValidate == null)
                        {
                            throw new Exception(String.Format("Could not find a valid 'Validate' method to this EntityBO ({0})", TypBOEntity.FullName));
                        }
                        object validateReturn = methodValidate.Invoke(entityBOInstance, null);

                        if (validateReturn != null && !((bool)validateReturn))
                        {
                            PropertyInfo propInvalidValues = TypBOEntity.GetProperty("InvalidValues");
                            List<InvalidValue> invalidValues = propInvalidValues.GetValue(entityBOInstance, null) as List<InvalidValue>;
                            if (invalidValues.Count > 0)
                            {
                                if (!Validate(invalidValues.ToArray()))
                                    return false;
                            }

                            PropertyInfo propInvalidEntity = TypBOEntity.GetProperty("InvalidEntityHeader");
                            List<InvalidValueBO> invalidEntityValues = propInvalidEntity.GetValue(entityBOInstance, null) as List<InvalidValueBO>;
                            if (invalidEntityValues.Count > 0)
                            {
                                this.ErrorsView.Visible = true;
                                StringBuilder erroMsg = new StringBuilder();
                                foreach (InvalidValueBO invalidValueBO in invalidEntityValues)
                                {
                                    erroMsg.AppendFormat(" - {0}<br>", invalidValueBO.Error);
                                }
                                this.ErrorsView.InnerHtml = erroMsg.ToString();
                                return false;
                            }
                        }

                        if (KPEventSaveClick != null)
                        {
                            KPEventSaveClick(entityBOInstance);
                        }
                        else
                        {
                            MethodInfo methodSave = TypBOEntity.GetMethod("SaveEntityBase");
                            object entitySaved = methodSave.Invoke(entityBOInstance, null);
                            DataSource = entitySaved;
                            AfterSaveEntity(DataSource as ActiveRecordBase);

                            if (KPEventAfterSaveClick != null)
                            {
                                KPEventAfterSaveClick(entityBOInstance);
                            }
                        }

                        var eventFormArgs = new KPFormEventArgs();
                        eventFormArgs.Entity = this.DataSourceAltered;
                        if (KPEventFormClosing != null)
                            KPEventFormClosing(this, eventFormArgs);

                        ClosePage();
                    }
                    else
                    {
                        return false;
                    }
                }

                return true;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null && ex.InnerException is KPExceptionValidator)
                {
                    KPExceptionValidator exception = ex.InnerException as KPExceptionValidator;
                    this.ErrorsView.Visible = true;
                    StringBuilder erroMsg = new StringBuilder();
                    foreach (InvalidValue error in exception.Erros)
                    {
                        erroMsg.AppendFormat(" - {0} <br>", error.Message);
                    }
                    this.ErrorsView.InnerHtml = erroMsg.ToString();

                    return false;
                }

                throw ex;
            }
        }