Data Annotations
This article will show you how to use Data Annotations with RadPropertyGrid.
We will discuss the following attributes:
They allow you to categorize different properties into separate groups, set some of them as read-only or define description for each one of them.
Creating RadPropertyGrid Application
First, for the purpose of this article, we will create a new Employee class with a couple of properties:
Example 1: Creating the Employee class
The declaration of RadPropertyGrid control inside the Grid:
Example 2: Declaring RadPropertyGrid
Once the class Employee is defined, you may use it for creating an object of this type and bind it to RadPropertyGrid:
Example 3: Binding Employee instance to RadPropertyGrid
After you run the application you should see the following:
Figure 1: RadPropertyGrid bound to an Employee instance
If you want to use Data Annotations in your application, you have to add a reference to the System.ComponentModel.DataAnnotations assembly.
Display
The Display attribute specifies localizable strings for data types and members that are used in the user interface. You can use its following properties:
Name: The value to be displayed instead of the property name.
Description: A description of the property. This will be shown in RadPropertyGrid's DescriptionPanel.
GroupName: Used when you want to categorize different properties into separate groups.
Order: Used when you want to set a property's display order.
Example 4 demonstrates how you can specify a Display attribute for a property:
Example 4: Setting Display attribute
Browsable
The Browsable attribute specifies whether a property should be displayed or not.
Example 5: Setting Browsable attribute
ReadOnly
The ReadOnly attribute specifies whether the property this attribute is bound to is read-only or read/write.
Example 6: Setting ReadOnly attribute
To see these attributes in action, let's modify the Employee class and see the result in RadPropertGrid.
Example 7: Modifying sample data
Here's the resulting RadPropertyGrid sorted alphabetically and in grouped mode: