public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle) { this._oldValue = this.Value; this._oldValueInitialize = true; base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle); ComboBoxCellEditingControl ctl = DataGridView.EditingControl as ComboBoxCellEditingControl; ctl.PropertyGirdAttribute = this._propertyRelatorAttribute; PropertyComboBoxEditorAttribute editorAttribute = (PropertyComboBoxEditorAttribute)this.PropertyEditorAttribute; ctl.DataSource = EnumDescConverter.Get(editorAttribute.Enum); if (this.Value != null) { ctl.SelectedValue = this.Value.ToString(); } else { ctl.SelectedValue = String.Empty; } }
protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter valueTypeConverter, System.ComponentModel.TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context) { string strValue = String.Empty; if (value != null) { PropertyComboBoxEditorAttribute editorAttribute = (PropertyComboBoxEditorAttribute)this.PropertyEditorAttribute; if (editorAttribute != null) { DataRow[] dr = EnumDescConverter.Get(editorAttribute.Enum).Select("Value='" + value + "'"); if (dr.Length > 0) { strValue = dr[0]["Text"].ToString(); } } else { strValue = value.ToString(); } } return(base.GetFormattedValue(strValue, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context)); }
/// <summary> /// 将当前属性值与默认属性值(如果有)对比 /// 如果不同,就把当前属性值粗体显示,否则常规显示 /// </summary> internal void ContrastDefaultValue() { if (this.DataGridView == null) { return; } //如果是只读,直接默认字体就行了,没必要加粗显示,也就没必要比较值 if (this.ReadOnly) { _propertyGridCell.Style.Font = this.DataGridView.DefaultCellStyle.Font; return; } object objCurrentValue = GetPropertyValue(); if (objCurrentValue == null) { return; } //这里必须判断当前值是否是空串,如果是空串,在下面进行类型转换时肯定会异常 //如尝试把空串转成Int if (objCurrentValue.ToString() == String.Empty) { return; } if (_iPropertyGirdCell.DefaultValueAttribute == null) { return; } bool equal = true; if (_iPropertyGirdCell.DefaultValueAttribute.Value.GetType().IsEnum) { PropertyComboBoxEditorAttribute editorAttribute = (PropertyComboBoxEditorAttribute)_iPropertyGirdCell.PropertyEditorAttribute; equal = Enum.Parse(editorAttribute.Enum, objCurrentValue.ToString()).Equals(_iPropertyGirdCell.DefaultValueAttribute.Value); } else { //设置过来的值是String,如 "300",而默认值是Property的类型,如Int //在这种情况下需要类型转换后再做比较判断,否则结果永远是false if (Type.Equals(objCurrentValue, _iPropertyGirdCell.DefaultValueAttribute.Value)) { equal = objCurrentValue.Equals(_iPropertyGirdCell.DefaultValueAttribute.Value); } else { IConvertible convertible = objCurrentValue as IConvertible; if (convertible != null) { equal = Convert.ChangeType( objCurrentValue, _iPropertyGirdCell.DefaultValueAttribute.Value.GetType() ).Equals(_iPropertyGirdCell.DefaultValueAttribute.Value); } else { equal = objCurrentValue.Equals(_iPropertyGirdCell.DefaultValueAttribute.Value); } } } if (equal) { _propertyGridCell.Style.Font = this.DataGridView.DefaultCellStyle.Font; } else { _propertyGridCell.Style.Font = new Font(this.DataGridView.DefaultCellStyle.Font, FontStyle.Bold); } }