Is it only possible to add categories to the .NET PropertyGrid by annotating my data class with attributes like CategoryAttribute
?
-
As opposed to what? Your question is unclear. – SLaks Jan 24 '11 at 14:31
4 Answers
You can use the CategoryAttribute
. The documentation states:
A new category can be created for any name by specifying the name of the category in the constructor for the
CategoryAttribute
.
Example:
[
Category("MyCategory"),
Description("Specifies something")
]
public string Something { //... }

- 117,245
- 29
- 183
- 222
-
Question was is the category attribute the only way, or is there another! :P - N – WraithNath Jan 24 '11 at 14:40
-
@WraithNath, really? I didn't get that from it, but I suppose you're right now that I read it again ;-) – Klaus Byskov Pedersen Jan 24 '11 at 14:42
-
The .NEt PropertyGrid takes only ONE object, what if I have several entities from my database, should I pass those objects into the ONE object whill I call PropSettings? – msfanboy Jan 24 '11 at 14:59
-
1The only thing i can think of is to create an object to use as a datasource, then implement the ICustomTypeDescriptor interface to return a set of custom attributes. Then you could perhaps return all property descriptors for all properties with the relevant category attributes attached. It took me quite a while to get it working though. I added some info to my blog on implementing it. http://wraithnath.blogspot.com/2011/01/implementing-icustomtypedescriptor-for.html – WraithNath Jan 24 '11 at 16:12
-
This is the first QA that comes up when searching for how to categorize properties. This is the answer most people will be looking for based on the question title. So thank you Klaus! – Hagelt18 Jun 23 '14 at 17:02
Doing this with ICustomTypeDescriptor and your own PropertyDescriptor class is quite easy and does not involve so much code.
This article Customized display of collection data in a PropertyGrid describes how to do this in detail. For adding categories you would also need to override the Category property in your PropertyDescriptor class.

- 4,036
- 1
- 23
- 43
It sounds like you're trying to mis-use the PropertyGrid to display multiple objects.
You can do that by writing a CustomTypeDescriptor
class which returns PropertyDescriptors
for each row you want to see in the grid, with appropriate CategoryAttribute
s.
This will take a substantial amount of work, but it is possible.

- 868,454
- 176
- 1,908
- 1,964
-
-
@msf: It completely depends on your development skills and the complexity of the objects. Basically, you need to make a bunch PropertyDescriptors. – SLaks Jan 24 '11 at 15:44
-
I do not have a collection of ONE type, I just have 2 diffeent objects like Customer and Department. Is that still possible with your answer/link? – msfanboy Jan 26 '11 at 10:47
-
@Peladao I have created now a ViewModel which wraps my both entities and delegate the property changes to my business classes! – msfanboy Jan 26 '11 at 11:37
-
@msfanboy: Cool! and yes, the article I mentioned shows a solution that works in general, not just for Collections. – Peladao Jan 26 '11 at 11:45
-
@Peladao Is it possible to display a collection of employees AND a collection of departments in a PropGrid? – msfanboy Jan 27 '11 at 09:31
-
Yes, based on the approach in the article: Create separate collections for employees and departments (you could perhaps even use the same class for both collections); derive propertydescriptor classes (1 for employees and 1 for departments); and then add a Departments property to the Organization class (just like the Employees property). – Peladao Jan 27 '11 at 10:04
There is a great set of classes to build your property grid content directly from code.

- 39,551
- 56
- 175
- 291