There is a hack available at: How to Set Attribute Value at Runtime-- and How to Work around a Silly Bug
private void SetPropertyGrid()
{
PropertyDescriptor descriptor = TypeDescriptor.GetProperties(typeof(Student))["Address"];
ReadOnlyAttribute attrib = (ReadOnlyAttribute)descriptor.Attributes[typeof(ReadOnlyAttribute)];
FieldInfo isReadOnly = attrib.GetType().GetField("isReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
isReadOnly.SetValue(attrib,true);
propertyGrid1.SelectedObject = new Student();
}
I was able to this code to change the ReadOnly
attribute value of the property. The statement propertyGrid1.SelectedObject = new Student();
can be replaced by propertyGrid1.SelectedObject = myStudent
i.e. you can modify the properties of an existing object.
Also, have a look at a similar question: Change Attribute's parameter at runtime