예제 #1
0
        private void btnAutoGenerated_Click(object sender, EventArgs e)
        {
            if (TargetGrid.EntityType == null)
            {
                XtraMessageBoxHelper.ShowWarning("没有指定表格的实体类型 EntityType");
                return;
            }
            string typeName = TargetGrid.EntityType.FullName;
            var    meta     = EntityMetadataFactory.Current.Get(typeName);

            if (meta == null)
            {
                XtraMessageBoxHelper.ShowWarning("没有指定实体元数据");
                return;
            }
            XtraMessageBoxHelper.ShowYesNoAndTips("确定自动生成列吗?", d =>
            {
                TargetGrid.OptionsBehavior.AutoPopulateColumns = false;
                TargetGrid.OptionsBehavior.Editable            = true;
                TargetGrid.Columns.Clear();

                for (int index = 0; index < meta.EntityFields.Count; index++)
                {
                    EntityField filed              = meta.EntityFields[index];
                    TreeListColumn column          = TargetGrid.Columns.Add();
                    column.Name                    = "tree_{0}_{1}".FS(typeName, filed.FieldName);
                    column.FieldName               = filed.FieldName;
                    column.Caption                 = filed.FieldComment;
                    column.VisibleIndex            = index + 1;
                    column.OptionsColumn.AllowEdit = false;
                    var dataType                   = EntityMetadata.GetFastProperty(TargetGrid.EntityType, filed.FieldName).Property.PropertyType;
                    if (dataType == typeof(DateTime) || dataType == typeof(DateTime?))
                    {
                        column.Format.FormatType   = DevExpress.Utils.FormatType.DateTime;
                        column.Format.FormatString = "yyyy-MM-dd HH:mm:ss";
                    }
                }
                Grid.RefreshData();
                TargetGrid.BestFitColumns();
            });
        }