/// <summary>把控件的值设置到实体属性上</summary> /// <param name="entity"></param> /// <param name="field"></param> /// <param name="control"></param> protected virtual void GetFormItem(IEntity entity, FieldItem field, Control control) { if (field == null || control == null) return; if (control is WebControl) { WebControl wc = control as WebControl; // 分控件处理 if (wc is TextBox) GetFormItemTextBox(entity, field, wc as TextBox); else if (wc is Label) GetFormItemLabel(entity, field, wc as Label); else if (wc is RadioButton) GetFormItemRadioButton(entity, field, wc as RadioButton); else if (wc is CheckBox) GetFormItemCheckBox(entity, field, wc as CheckBox); else if (wc is ListControl) GetFormItemListControl(entity, field, wc as ListControl); else { Object v = null; if (GetControlValue(control, out v) && !Object.Equals(entity[field.Name], v)) SetEntityItem(entity, field, v); } } else { Object v = null; if (GetControlValue(control, out v) && !Object.Equals(entity[field.Name], v)) SetEntityItem(entity, field, v); } }
/// <summary>已重载。</summary> /// <param name="entity"></param> /// <param name="field">字段</param> /// <param name="control"></param> protected override void GetFormItem(IEntity entity, FieldItem field, Control control) { // 特殊处理附件。要求控件类型是文件上传控件,字段类型是整型 if (control is FileUpload && field.Type == typeof(Int32)) { GetFormItemFileUpload(entity, field, control as FileUpload); return; } base.GetFormItem(entity, field, control); }
/// <summary>外部=>实体,从外部读取指定实体字段的信息</summary> /// <param name="entity">实体对象</param> /// <param name="item">实体字段</param> protected override void ReadItem(IEntity entity, FieldItem item) { Control control = FindControlByField(item); if (control == null) return; //try //{ GetFormItem(entity, item, control); //} //catch (Exception ex) //{ // throw new XCodeException("读取" + item.Name + "的数据时出错!" + ex.Message, ex); //} }
static Boolean UseParam(FieldItem fi) { return (fi.Length <= 0 || fi.Length >= 4000) && (fi.Type == typeof(Byte[]) || fi.Type == typeof(String)); }
internal static WhereExpression MakeCondition(FieldItem field, Object value, String action) { //IEntityOperate op = EntityFactory.CreateOperate(field.Table.EntityType); //return new WhereExpression(String.Format("{0}{1}{2}", op.FormatName(field.ColumnName), action, op.FormatValue(field, value))); return(field == null ? new WhereExpression() : field.CreateExpression(action, value)); }
/// <summary>将实体信息添充至ListControl</summary> /// <param name="entity"></param> /// <param name="field"></param> /// <param name="control"></param> private void SetListControl(IEntity entity, FieldItem field, ListControl control) { Type type = field.Type; Object value = entity[field.Name]; if (type == typeof(int)) control.SelectedIndex = (int)value; else { control.SelectedValue = value; if (control.SelectedIndex == -1) { if (control is ListBox) { (control as ListBox).SelectedItem = value; } else if (control is ComboBox) { (control as ComboBox).SelectedItem = value; } } } }
/// <summary>查找字段对应的控件</summary> /// <param name="field"></param> /// <returns></returns> protected virtual Control FindControlByField(FieldItem field) { String name = ItemPrefix + field.Name; //Control control = FieldInfoX.GetValue<Control>(Container, name); // 这里可能极为不完善,需要找到WinForm中控件默认命名方式 return FindControlInContainer(name); }
/// <summary>将实体信息添充至TextBoxBase</summary> /// <param name="entity"></param> /// <param name="field"></param> /// <param name="control"></param> private void SetTextBoxBase(IEntity entity, FieldItem field, TextBoxBase control) { Type type = field.Type; Object value = entity[field.Name]; if (value != null) control.Text = "" + value; else { if (field.IsNullable == true) control.Text = ""; else control.Text = field.DefaultValue; } }
/// <summary>将实体信息添充至DateTimePicker</summary> /// <param name="entity"></param> /// <param name="field"></param> /// <param name="control"></param> private void SetDateTimePicker(IEntity entity, FieldItem field, DateTimePicker control) { Type type = field.Type; Object value = entity[field.Name]; DateTime valueDateTime = DateTime.MinValue; //不需要判断 hfmedical //if (type != typeof(DateTime)) DateTime.TryParse(value.ToString(), out valueDateTime); control.Value = valueDateTime; }
/// <summary>获取ListControl填充实体类</summary> /// <param name="entity"></param> /// <param name="field"></param> /// <param name="control"></param> private void GetListControl(IEntity entity, FieldItem field, ListControl control) { Object v = control.SelectedValue; if (v == null) v = control.Text; if (!Object.Equals(entity[field.Name], v)) SetEntityItem(entity, field, v); }
/// <summary>实体=>外部,把指定实体字段的信息写入到外部</summary> /// <param name="entity">实体对象</param> /// <param name="item">实体字段</param> protected override void WriteItem(IEntity entity, FieldItem item) { Control control = FindControlByField(item); if (control == null) return; Boolean canSave = true; try { SetFormItem(entity, item, control, canSave); } catch (Exception ex) { throw new XCodeException("设置" + item.Name + "的数据时出错!" + ex.Message, ex); } }
/// <summary>查找字段对应的控件</summary> /// <param name="field"></param> /// <returns></returns> protected virtual Control FindControlByField(FieldItem field) { return FindControl(ItemPrefix + field.Name); }
/// <summary>获取DateTimePicker填充实体类</summary> /// <param name="entity"></param> /// <param name="field"></param> /// <param name="control"></param> private void GetDateTimePicker(IEntity entity, FieldItem field, DateTimePicker control) { Object v = control.Value; if (!Object.Equals(entity[field.Name], v)) SetEntityItem(entity, field, v); }
/// <summary>设置控件的不允许空</summary> /// <param name="field"></param> /// <param name="control"></param> /// <param name="canSave"></param> protected virtual void SetNotAllowNull(FieldItem field, Control control, Boolean canSave) { if (field.IsNullable) return; // Label后面不需要 if (control is Label) return; var lc = new LiteralControl(); lc.Text = "<font style='color:#FF0000;font-size:16pt;'> *</font>"; Int32 p = control.Parent.Controls.IndexOf(control); // 有时候可能无法添加,但是不影响使用,应该屏蔽异常 try { control.Parent.Controls.AddAt(p + 1, lc); } catch { } }
/// <summary>单选框</summary> /// <param name="entity"></param> /// <param name="field"></param> /// <param name="control"></param> /// <param name="canSave"></param> protected virtual void SetFormItemRadioButton(IEntity entity, FieldItem field, RadioButton control, Boolean canSave) { List<RadioButton> list = new List<RadioButton>(); // 找到同一级同组名的所有单选 foreach (Control item in control.Parent.Controls) { if (!(item is RadioButton)) continue; RadioButton rb = item as RadioButton; if (rb.GroupName != control.GroupName) continue; list.Add(rb); } if (list.Count < 1) return; // 特殊处理数字 if (field.Type == typeof(Int32)) { Int32 id = (Int32)entity[field.Name]; if (id < 0 || id >= list.Count) id = 0; for (int i = 0; i < list.Count; i++) { list[i].Checked = (i == id); } } else { String value = String.Empty + entity[field.Name]; foreach (RadioButton item in list) { item.Checked = item.Text == value; } } }
/// <summary>列表框</summary> /// <param name="entity"></param> /// <param name="field"></param> /// <param name="control"></param> /// <param name="canSave"></param> protected virtual void SetFormItemListControl(IEntity entity, FieldItem field, ListControl control, Boolean canSave) { if (control.Items.Count < 1 || !String.IsNullOrEmpty(control.DataSourceID)) { control.DataBind(); // 这个赋值会影响RequiresDataBinding,而RequiresDataBinding会导致列表控件在OnPreRender阶段重新绑定,造成当前设定的值丢失。 //control.AppendDataBoundItems = false; } if (control.Items.Count < 1) return; String value = String.Empty + entity[field.Name]; try { control.SelectedValue = value; if (control.GetType() == typeof(DropDownList) || control.GetType() == typeof(ListControl)) { /* * 对于ListControl和DropDownList,仅PostBack时有可能抛出异常,初次打开有可能在PerformDataBinding中抛出异常,所以要做额外检测 * * 因为DropDownList.SelectedIndex get时有可能修改Items[0].Selected, 所以下面代码最好避免访问SelectedIndex,SelectedValue * * 对于XControl.DropDownList始终没问题 * */ var selected = false; for (int i = 0; i < control.Items.Count; i++) { var item = control.Items[i]; if (item.Selected) { selected = item.Value == value; break; } } if (!selected) { // 没有任何选中项或选中项不是设置的值 throw new ArgumentException(); } } } catch (ArgumentException) { var li = control.Items.FindByValue(value); if (li == null) { li = new ListItem(value, value); control.Items.Add(li); } control.ClearSelection(); li.Selected = true; } }
/// <summary>复选框</summary> /// <param name="entity"></param> /// <param name="field"></param> /// <param name="control"></param> /// <param name="canSave"></param> protected virtual void SetFormItemCheckBox(IEntity entity, FieldItem field, CheckBox control, Boolean canSave) { Type type = field.Type; if (type == typeof(Boolean)) control.Checked = (Boolean)entity[field.Name]; else if (type == typeof(Int32)) control.Checked = (Int32)entity[field.Name] != 0; else control.Checked = entity[field.Name] != null; }
internal static Expression CreateField(FieldItem field, String action, Object value) => field == null ? new Expression() : new FieldExpression(field, action, value);
/// <summary>获取NumericUpDown填充实体类</summary> /// <param name="entity"></param> /// <param name="field"></param> /// <param name="control"></param> private void GetNumericUpDown(IEntity entity, FieldItem field, NumericUpDown control) { Object v = control.Value; if (!Object.Equals(entity[field.Name], v)) SetEntityItem(entity, field, v); }
/// <summary>从上传控件中取数据</summary> /// <param name="entity"></param> /// <param name="field">字段</param> /// <param name="control"></param> protected virtual void GetFormItemFileUpload(IEntity entity, FieldItem field, FileUpload control) { }
/// <summary>获取Label填充实体类</summary> /// <param name="entity"></param> /// <param name="field"></param> /// <param name="control"></param> private void GetLabel(IEntity entity, FieldItem field, Label control) { Object v = control.Text; if (!Object.Equals(entity[field.Name], v)) SetEntityItem(entity, field, v); }
/// <summary>构造字段表达式</summary> /// <param name="field"></param> /// <param name="action"></param> /// <param name="value"></param> public FieldExpression(FieldItem field, String action, Object value) { Field = field; Action = action; Value = value; }
/// <summary>把实体成员的值设置到控件上</summary> /// <param name="entity"></param> /// <param name="field"></param> /// <param name="control"></param> /// <param name="canSave"></param> protected virtual void SetFormItem(IEntity entity, FieldItem field, Control control, Boolean canSave) { if (field == null || control == null) return; String toolTip = String.IsNullOrEmpty(field.Description) ? field.Name : field.Description; if (field.IsNullable) toolTip = String.Format("请填写{0}!", toolTip); else toolTip = String.Format("必须填写{0}!", toolTip); if (control is Control) { Control wc = control as Control; // 设置ToolTip SetToolTip(wc, toolTip); //// 必填项 //if (!field.IsNullable) SetNotAllowNull(field, control, canSave); // 设置只读,只有不能保存时才设置,因为一般都不是只读,而前端可能自己设置了控件为只读,这种情况下这里再设置就会修改前端的意思 if (!canSave) { SetControlEnable(wc, canSave); } //文本框、RichTextBox ,MaskedTextBox if (control is TextBoxBase) SetTextBoxBase(entity, field, control as TextBoxBase); //单选、多选 else if (control is ButtonBase) SetButtonBase(entity, field, control as ButtonBase); //日期和时间 else if (control is DateTimePicker) SetDateTimePicker(entity, field, control as DateTimePicker); //数字 else if (control is NumericUpDown) SetNumericUpDown(entity, field, control as NumericUpDown); //ListBox、ComboBox、CheckedListBox else if (control is ListControl) SetListControl(entity, field, control as ListControl); //Label else if (control is Label) SetLabel(entity, field, control as Label); else throw new Exception("不受支持的控件类型!"); } }
internal static WhereExpression MakeCondition(FieldItem field, Object value, String action) { //IEntityOperate op = EntityFactory.CreateOperate(field.Table.EntityType); //return new WhereExpression(String.Format("{0}{1}{2}", op.FormatName(field.ColumnName), action, op.FormatValue(field, value))); return field == null ? new WhereExpression() : field.CreateExpression(action, value); }
/// <summary>将实体信息添充至ButtonBase</summary> /// <param name="entity"></param> /// <param name="field"></param> /// <param name="control"></param> private void SetButtonBase(IEntity entity, FieldItem field, ButtonBase control) { Type type = field.Type; Object value = entity[field.Name]; Boolean isChecked = false; if (type == typeof(bool)) isChecked = (Boolean)value; else if (type == typeof(int)) isChecked = (int)value == 0 ? false : true; else if (type == typeof(string)) { String valueString = (String)value; if (String.IsNullOrEmpty(valueString)) isChecked = false; else isChecked = valueString == "1" || valueString.EqualIgnoreCase("true") ? true : false; } if (control is RadioButton) ((RadioButton)control).Checked = isChecked; else if (control is CheckBox) ((CheckBox)control).Checked = isChecked; else throw new Exception("不接爱Button控件!"); }
/// <summary> /// /// 是否应考滤值转换,如列表,单选,多选其显示文字或写value不同 /// </summary> /// <param name="entity"></param> /// <param name="field"></param> /// <param name="control"></param> private void GetFormItem(IEntity entity, FieldItem field, Control control) { if (field == null || control == null) return; if (control is System.Windows.Forms.Control) { //文本框、RichTextBox ,MaskedTextBox if (control is TextBoxBase) GetTextBoxBase(entity, field, control as TextBoxBase); //单选、多选 else if (control is ButtonBase) GetButtonBase(entity, field, control as ButtonBase); //日期和时间 else if (control is DateTimePicker) GetDateTimePicker(entity, field, control as DateTimePicker); //数字 else if (control is NumericUpDown) GetNumericUpDown(entity, field, control as NumericUpDown); //ListBox、ComboBox、CheckedListBox else if (control is ListControl) GetListControl(entity, field, control as ListControl); //Label else if (control is Label) GetLabel(entity, field, control as Label); else throw new Exception("不受支持的控件类型!"); //是否需要从中读出记录集 //ListView //DataGridView } }
/// <summary>将实体信息添充至NumericUpDown</summary> /// <param name="entity"></param> /// <param name="field"></param> /// <param name="control"></param> private void SetNumericUpDown(IEntity entity, FieldItem field, NumericUpDown control) { Type type = field.Type; Object value = entity[field.Name]; Decimal valuedecimal; Decimal.TryParse(value.ToString(), out valuedecimal); control.Value = valuedecimal; }
/// <summary>设置实体类值</summary> /// <param name="entity"></param> /// <param name="field"></param> /// <param name="value"></param> void SetEntityItem(IEntity entity, FieldItem field, Object value) { // 先转为目标类型 value = TypeX.ChangeType(value, field.Type); // 如果是字符串,并且为空,则让它等于实体里面的值,避免影响脏数据 if (field.Type == typeof(String) && String.IsNullOrEmpty((String)value) && String.IsNullOrEmpty((String)entity[field.Name])) value = entity[field.Name]; entity.SetItem(field.Name, value); }
/// <summary>将实体信息添充至Label</summary> /// <param name="entity"></param> /// <param name="field"></param> /// <param name="control"></param> private void SetLabel(IEntity entity, FieldItem field, Label control) { Type type = field.Type; Object value = entity[field.Name]; if (type == typeof(DateTime)) control.Text = ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss"); else control.Text = value.ToString(); }
/// <summary>获取TextBoxBase填充实体类</summary> /// <param name="entity"></param> /// <param name="field"></param> /// <param name="control"></param> private void GetTextBoxBase(IEntity entity, FieldItem field, TextBoxBase control) { Object v = control.Text; if (!Object.Equals(entity[field.Name], v)) SetEntityItem(entity, field, v); }
void InitFields() { var bt = _Table; var table = DAL.CreateTable(); _DataTable = table; table.TableName = bt.Name; table.Name = EntityType.Name; table.DbType = bt.DbType; table.IsView = bt.IsView; table.Description = Description; //table.ConnName = ConnName; var allfields = new List <FieldItem>(); var fields = new List <FieldItem>(); var pkeys = new List <FieldItem>(); foreach (var item in GetFields(EntityType)) { var fi = item; allfields.Add(fi); if (fi.IsDataObjectField) { fields.Add(fi); var f = table.CreateColumn(); fi.Fill(f); table.Columns.Add(f); } if (fi.PrimaryKey) { pkeys.Add(fi); } if (fi.IsIdentity) { _Identity = fi; } if (fi.Master) { _Master = fi; } } // 先完成allfields才能专门处理 foreach (var item in allfields) { if (!item.IsDynamic) { // 如果不是数据字段,则检查绑定关系 var map = item.Map; if (map != null) { // 找到被关系映射的字段,拷贝相关属性 var fi = allfields.FirstOrDefault(e => e.Name.EqualIgnoreCase(map.Name)); if (fi != null) { if (item.OriField == null) { item.OriField = fi; } if (item.DisplayName.IsNullOrEmpty()) { item.DisplayName = fi.DisplayName; } if (item.Description.IsNullOrEmpty()) { item.Description = fi.Description; } item.ColumnName = fi.ColumnName; } } } } if (_Indexes != null && _Indexes.Length > 0) { foreach (var item in _Indexes) { var di = table.CreateIndex(); item.Fill(di); if (table.GetIndex(di.Columns) != null) { continue; } // 如果索引全部就是主键,无需创建索引 if (table.GetColumns(di.Columns).All(e => e.PrimaryKey)) { continue; } table.Indexes.Add(di); } } if (_Relations != null && _Relations.Length > 0) { foreach (var item in _Relations) { var dr = table.CreateRelation(); item.Fill(dr); if (table.GetRelation(dr) == null) { table.Relations.Add(dr); } } } // 不允许为null _AllFields = allfields.ToArray(); _Fields = fields.ToArray(); _PrimaryKeys = pkeys.ToArray(); }
/// <summary> /// 获取ButtonBase填充实体类 /// 支持RadioButton,CheckBox /// </summary> /// <param name="entity"></param> /// <param name="field"></param> /// <param name="control"></param> private void GetButtonBase(IEntity entity, FieldItem field, ButtonBase control) { Object v; if (control is RadioButton) v = ((RadioButton)control).Checked; else if (control is CheckBox) v = ((CheckBox)control).Checked; else throw new Exception("不接爱Button控件!"); if (field.Type == typeof(Int32)) v = (Boolean)v ? 1 : 0; if (!Object.Equals(entity[field.Name], v)) SetEntityItem(entity, field, v); }
/// <summary> /// 根据字段类型和长度获取对应类型的随机数据 /// </summary> /// <param name="fild">字段对象</param> /// <returns>对应的随机数据</returns> private static object GetRandomValue(FieldItem fild) { switch (Type.GetTypeCode(fild.Field.DataType)) { case TypeCode.Boolean: return RandomHelper.GetRandomBool(); case TypeCode.Byte: return RandomHelper.GetRandomByte(); case TypeCode.Char: return RandomHelper.GetRandomChar(); case TypeCode.DateTime: return RandomHelper.GetRandomDateTime(); case TypeCode.Decimal: return RandomHelper.GetRandomDouble(0, NeedCount * 10.1); case TypeCode.Double: return RandomHelper.GetRandomDouble(0, NeedCount * 10.1); case TypeCode.Int16: return RandomHelper.GetRandomInt(1, UInt16.MaxValue); case TypeCode.Int32: return RandomHelper.GetRandomInt(1, NeedCount * 50); case TypeCode.Int64: return RandomHelper.GetRandomInt(1, NeedCount * 100); case TypeCode.SByte: return RandomHelper.GetRandomInt(1, 127); case TypeCode.Single: return RandomHelper.GetRandomDouble(0, NeedCount * 10.1); case TypeCode.String: return RandomHelper.GetRandomString((int)(fild.Length * RandomHelper.GetRandomDouble(0.2, 0.7))); case TypeCode.UInt16: return RandomHelper.GetRandomInt(1, UInt16.MaxValue); case TypeCode.UInt32: return RandomHelper.GetRandomInt(1, NeedCount * 50); case TypeCode.UInt64: return RandomHelper.GetRandomInt(1, NeedCount * 100); default: return string.Empty; } }
//Boolean hasInitFields = false; void InitFields() { //if (hasInitFields) return; //hasInitFields = true; var bt = Table; var table = DAL.CreateTable(); _DataTable = table; table.Name = bt.Name; table.Alias = EntityType.Name; table.DbType = bt.DbType; table.IsView = bt.IsView; table.Description = Description; var allfields = new List <FieldItem>(); var fields = new List <FieldItem>(); var pkeys = new List <FieldItem>(); //var pis = EntityType.GetProperties(); foreach (var item in GetFields(EntityType)) { //// 排除索引器 //if (item.GetIndexParameters().Length > 0) continue; //var fi = new Field(this, item); var fi = item; allfields.Add(fi); if (fi.IsDataObjectField) { fields.Add(fi); var f = table.CreateColumn(); fi.Fill(f); table.Columns.Add(f); } if (fi.PrimaryKey) { pkeys.Add(fi); } if (fi.IsIdentity) { _Identity = fi; } } if (_Indexes != null && _Indexes.Length > 0) { foreach (var item in _Indexes) { var di = table.CreateIndex(); item.Fill(di); if (ModelHelper.GetIndex(table, di.Columns) != null) { continue; } //// 如果这个索引的唯一字段是主键,则无需建立索引 //var column = table.GetColumn(di.Columns[0]); //if (column == null || (di.Columns.Length == 1 && column.PrimaryKey)) continue; // 如果索引全部就是主键,无需创建索引 if (table.GetColumns(di.Columns).All(e => e.PrimaryKey)) { continue; } //// 判断主键 //IDataColumn[] dcs = table.GetColumns(di.Columns); //if (Array.TrueForAll<IDataColumn>(dcs, dc => dc.PrimaryKey)) //{ // di.PrimaryKey = true; // di.Unique = true; //} table.Indexes.Add(di); } } if (_Relations != null && _Relations.Length > 0) { foreach (var item in _Relations) { var dr = table.CreateRelation(); item.Fill(dr); Boolean exists = false; foreach (var elm in table.Relations) { if (!String.Equals(elm.Column, dr.Column, StringComparison.OrdinalIgnoreCase)) { continue; } if (!String.Equals(elm.RelationTable, dr.RelationTable, StringComparison.OrdinalIgnoreCase)) { continue; } if (!String.Equals(elm.RelationColumn, dr.RelationColumn, StringComparison.OrdinalIgnoreCase)) { continue; } exists = true; break; } if (!exists) { table.Relations.Add(dr); } } } //if (allfields != null && allfields.Count > 0) _AllFields = allfields.ToArray(); //if (fields != null && fields.Count > 0) _Fields = fields.ToArray(); //if (pkeys != null && pkeys.Count > 0) _PrimaryKeys = pkeys.ToArray(); // 不允许为null _AllFields = allfields.ToArray(); _Fields = fields.ToArray(); _PrimaryKeys = pkeys.ToArray(); }