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.View.OptionsBehavior.Editable = true; TargetGrid.View.Columns.Clear(); #region 自动选择 GridColumn colSelected = new GridColumn(); colSelected.FieldName = "Selected"; colSelected.Caption = "选择"; colSelected.VisibleIndex = 0; colSelected.OptionsColumn.AllowEdit = true; colSelected.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; TargetGrid.View.Columns.Add(colSelected); #endregion for (int index = 0; index < meta.EntityFields.Count; index++) { EntityField filed = meta.EntityFields[index]; GridColumn column = new GridColumn(); column.Name = "grid_{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.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime; column.DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss"; } TargetGrid.View.Columns.Add(column); } Grid.RefreshData(); TargetGrid.View.BestFitColumns(); }); }
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(); }); }