C # dynamically changing the properties displayed in PropertyGrid during design
Method 1:
////// Processing object attributes ///Public class PropertyHandle {# region reflection control read-only and visible attributes // SetPropertyVisibility (obj, name, true); // obj refers to your SelectObject, "name" is an attribute of your SelectObject. // of course, after you call these two methods, re-select tobject. ////// Read-only attribute control through reflection ////////////Public static void SetPropertyReadOnly (object obj, string propertyName, bool readOnly) {Type type = typeof (ReadOnlyAttribute); PropertyDescriptorCollection props = TypeDescriptor. getProperties (obj); AttributeCollection attrs = props [propertyName]. attributes; FieldInfo Attributes = type. getField (isReadOnly, BindingFlags. instance | BindingFlags. nonPublic | BindingFlags. createInstance); created. setValue (attrs [type], readOnly );}////// Use reflection to control whether the attribute is visible ////////////Public static void SetPropertyVisibility (object obj, string propertyName, bool visible) {Type type = typeof (BrowsableAttribute); PropertyDescriptorCollection props = TypeDescriptor. getProperties (obj); AttributeCollection attrs = props [propertyName]. attributes; FieldInfo Attributes = type. getField (browsable, BindingFlags. instance | BindingFlags. nonPublic); optional. setValue (attrs [type], visible) ;}# endregion}For example, if an object class TestEntity has an attribute such as IdentityColumnInfo, the corresponding object class of the auto-increment column is as follows:
Public class IdentityColumnEntity {private bool isIncrementColumn ;////// Whether it is an auto-increment column ///[Browsable (true)] [Category (basic)] [DisplayName (whether it is an auto-incrementing column)] [ReadOnly (false)] [DefaultValue (false)] public bool IsIncrementColumn {set {isIncrementColumn = value;} get {return isIncrementColumn;} private Int64 identityIncrement ;////// ID increment ///[Browsable (true)] [Category (basic)] [DisplayName (ID increment)] [ReadOnly (false)] [Description (ID increment attribute specifies the value added based on the existing largest row id value when Microsoft SQL Server generates the id value for the inserted row. The ID increment must be a non-zero integer with the number of digits equal to or less than 10.)] Public Int64 IdentityIncrement {set {identityIncrement = value;} get {return identityIncrement;} private Int64 ident_Seed ;////// Identify the seed ///[Browsable (true)] [Category (basic)] [DisplayName (identification seed)] [ReadOnly (false)] [Description (indicating the initial row value of the ID column. The seed must be an integer with the number of digits equal to or less than 10.)] Public Int64 Ident_Seed {set {ident_Seed = value;} get {return ident_Seed ;}}}The object TestEntity code is as follows:
Public class TestEntity {private IdentityColumnEntity identityColumnInfo ;////// Auto-increment column ///[Category (Extended information)] [DisplayName (auto-incrementing column information)] [ReadOnlyAttribute (true)] [XmlIgnore] [Browsable (true)] // [XmlAttribute] public IdentityColumnEntity IdentityColumnInfo {get {if (identityColumnInfo! = Null & identityColumnInfo. isIncrementColumn) {PropertyHandle. setPropertyReadOnly (identityColumnInfo, Ident_Seed, true); PropertyHandle. setPropertyReadOnly (identityColumnInfo, IdentityIncrement, true); PropertyHandle. setPropertyReadOnly (identityColumnInfo, IsIncrementColumn, true); PropertyHandle. setPropertyReadOnly (this, IsInsert, true); PropertyHandle. setPropertyReadOnly (this, IsUpdate, true) ;}return identityColumnInfo ;}set {identityColumnInfo = value ;}}////// Whether to insert ///[Category (Maintenance Information)] [DisplayName (inserted or not)] [ReadOnlyAttribute (false)] [XmlAttribute] public bool IsInsert {get; set ;}////// Update ///[Category (Maintenance Information)] [DisplayName (updated or not)] [ReadOnlyAttribute (false)] [XmlAttribute] public bool IsUpdate {get; set ;}}You can use PropertyHandle. SetPropertyReadOnly to achieve the following results: