Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:

I have a field in a class which the user can modify via a PropertyGrid.

So I made a public property which can be accessed via the PropertyGrid and the user can assign a value.

The problem is that I want to make sure that the value assigned by the user via the PropertyGrid should be readonly i.e. the property should not be modified through the object reference.

In other words I somehow want to make sure that user only can assign the value of the property via the PropertyGrid and anyone who is using this object should not be able to modify it directly.

This property should be readonly but from the PorpertyGrid one should be able to modify it. Is there any way to achieve this? Is there any way to modify private properties via the PropertyGrid?
Updated 26-Oct-10 4:07am

1 solution

Unfortunately no. If the property grid can access it, then any other code can too.

But there is a trick you can use that's specific to the property grid. Remove the public property and replace it with a dynamic runtime property using ICustomTypeDescriptor and TypeDescriptionProvider. I've got an article here that explains how to do this:[^]

It is not a very simple task but it's not too complex either.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900