public void GenFields(object sender, EventArgs e) { if (formView == null) { formView = this.Component as AjaxFormView; } if (formView != null && !string.IsNullOrEmpty(formView.DataSourceID)) { if (formView.Fields.Count == 0) { WebDataSource wds = formView.GetObjByID(formView.DataSourceID) as WebDataSource; DataTable srcTable = GetDesignTable(wds); if (srcTable != null) { DataTable ddTable = DBUtils.GetDataDictionary(wds, true).Tables[0]; bool genAlternateColumns = (MessageBox.Show("generate alternate columns", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes); if (host == null) { host = (IDesignerHost)GetService(typeof(IDesignerHost)); } if (svcCompChange == null) { svcCompChange = (IComponentChangeService)this.GetService(typeof(IComponentChangeService)); } DesignerTransaction trans = host.CreateTransaction("Generate Fields"); for (int i = 0; i < srcTable.Columns.Count; i++) { AjaxFormField field = new AjaxFormField(); field.FieldControlId = string.Format("ctrl{0}", srcTable.Columns[i].ColumnName); field.Caption = Caption(srcTable.Columns[i].ColumnName, ddTable); field.IsKeyField = IsKeyField(srcTable.Columns[i].ColumnName, srcTable.PrimaryKey); field.DataField = srcTable.Columns[i].ColumnName; if (genAlternateColumns && i % 2 == 1) { field.NewLine = false; } this.FieldTypeSelector(srcTable.Columns[i].DataType, field); svcCompChange.OnComponentChanging(formView, null); formView.Fields.Add(field); svcCompChange.OnComponentChanged(formView, null, null, null); } trans.Commit(); MessageBox.Show("generate fields successful!"); } } } }
string GenFieldControl(AjaxFormField field, int LabelWidth, int colSpan) { StringBuilder builder = new StringBuilder(); if (field.Visible) { builder.Append("{"); if (field.Editor == ExtGridEditor.RefVal) { builder.AppendFormat("fieldLabel:'{0}',name:'{1}',", field.Caption, field.DataField, string.IsNullOrEmpty(field.Formatter) ? "" : string.Format("format:'{0}',", field.Formatter), field.Width, LabelWidth, colSpan > 1 ? string.Format("colspan:{0},", colSpan) : ""); } else { builder.AppendFormat("fieldLabel:'{0}',id:'{5}',name:'{1}',{2}width:{3},labelWidth:{4},disabled:true,disabledCls:'info-x-item-disabled',", field.Caption, field.DataField, string.IsNullOrEmpty(field.Formatter) ? "" : string.Format("format:'{0}',", field.Formatter), field.Width, LabelWidth, this.ID + field.DataField); } if (field.ReadOnly) { builder.Append("readOnly:'true',"); } switch (field.Editor) { case ExtGridEditor.TextBox: builder.Append("xtype:'textfield'"); break; case ExtGridEditor.TextArea: builder.Append("xtype:'textarea'"); builder.AppendFormat(",height:{0}", field.Height); break; case ExtGridEditor.NumberField: builder.Append("xtype:'numberfield'"); if (field.Formatter != null) { string decimalPrecision = "0"; try { string[] formatters = field.Formatter.Split(new char[] { ',', '.' }); if (formatters.Length > 1) { decimalPrecision = formatters[1].Length.ToString(); } } finally { } builder.AppendFormat(",decimalPrecision:{0}", decimalPrecision); } break; case ExtGridEditor.CheckBox: builder.Append("xtype:'checkbox'"); break; case ExtGridEditor.ComboBox: builder.Append("xtype:'infoCombo',embededIn:'form',"); if (!string.IsNullOrEmpty(field.EditControlId)) { ExtComboBox cmb = this.GetObjByID(field.EditControlId) as ExtComboBox; if (cmb != null) { builder.Append(cmb.GenComboBoxConfig(null, field.DataField, true)); } } break; case ExtGridEditor.DateTimePicker: builder.Append("xtype:'datefield'"); break; case ExtGridEditor.RefVal: ExtRefVal refVal = this.GetObjByID(field.EditControlId) as ExtRefVal; if (refVal != null) { builder.Append(refVal.GenRefValConfig(field.DataField, field.Caption, LabelWidth, this.ID, field.AllowNull, field.ValidType, field.ValidMethod, field.ValidText)); } //builder.Append("xtype:'infoRefVal'"); //builder.AppendFormat("id: '{0}',", field.DataField);//refVal.ClientID + "TextBox" //builder.Append("xtype:'panel',"); //builder.Append("layout: 'column',"); ////builder.AppendFormat("width: 80,", refVal.Width); //builder.Append("baseCls: \"x-plain\","); //builder.Append("items: [{"); //builder.Append("disabled:true,disabledClass:'info-x-item-disabled',"); //builder.Append("xtype: 'textfield',"); ////builder.Append("baseCls: \"x-plain\","); //builder.Append("columnWidth: 0.8"); //builder.Append("},{"); ////builder.AppendFormat("id: '{0}',", field.DataField + "Button");//refVal.ClientID + "TextBox" //builder.Append("xtype: 'button',"); //builder.Append("disabled:true,disabledClass:'info-x-item-disabled',"); ////builder.Append("baseCls: \"x-plain\","); //builder.Append("columnWidth: 0.2,"); //builder.Append("listeners: {"); //builder.Append("'click': function(){"); //builder.Append("Ext.MessageBox.alert('Message', 'Test ! ');"); //builder.Append("}"); //builder.Append("}"); //builder.Append("}]"); break; } if ((!field.AllowNull || field.ValidType != ValidateType.None) && field.Editor != ExtGridEditor.RefVal) { builder.AppendFormat(",{0}", ExtValidator.GenValidateConfig(field.AllowNull, field.ValidType, field.ValidMethod, field.ValidText)); } builder.Append("}"); } return builder.ToString(); }
void FieldTypeSelector(Type fieldType, AjaxFormField field) { if (fieldType == typeof(uint) || fieldType == typeof(UInt16) || fieldType == typeof(UInt32) || fieldType == typeof(UInt64) || fieldType == typeof(int) || fieldType == typeof(Int16) || fieldType == typeof(Int32) || fieldType == typeof(Int64)) { field.FieldType = "int"; field.ValidType = ValidateType.Int; } else if (fieldType == typeof(Single) || fieldType == typeof(double) || fieldType == typeof(decimal)) { field.FieldType = "float"; field.ValidType = ValidateType.Float; } else if (fieldType == typeof(string)) { field.FieldType = "string"; } else if (fieldType == typeof(bool)) { field.FieldType = "boolean"; } else if (fieldType == typeof(DateTime)) { field.FieldType = "date"; field.Formatter = "Y/m/d";//"Ext.util.Format.dateRenderer('Y/m/d')"; field.Editor = ExtGridEditor.DateTimePicker; } }