1. Add the property grid control to the toolbox.
By default, the property grid control is not displayed in the toolbox, so you need to add it manually.
Figure 1 add property grid to the Toolbox (right-click the toolbox and choose Add/Remove items)
Figure 2 select the propertygrid control in the Custom toolkit dialog box
2. Write a custom class and bind it with the propertygrid control.
Using system;
Using system. componentmodel;
Namespace propertygriddemo
{
[Defaproperpropertyattribute ("name")]
Public Class Customer
{
Private string name;
Private string email;
Private string mark;
[Categoryattribute ("user information"), descriptionattribute ("set consumer name")]
Public string name
{
Get
{
Return name;
}
Set
{
Name = value;
}
}
[Categoryattribute ("user information"), descriptionattribute ("set consumer email address")]
Public String email
{
Get
{
Return email;
}
Set
{
Email = value;
}
}
[Categoryattribute ("Remarks"), descriptionattribute ("Remarks")]
Public String mark
{
Get
{
Return mark;
}
Set
{
Mark = value;
}
}
Public customer ()
{
}
}
}
First, you must reference system when defining a custom type. the componentmodel namespace uses some attribute classes in the namespace. In the preceding example, the defaultpropertyattribute, categoryattribute, and descriptionattribute attributes are used.
Defaultpropertyattribute specifies the default attribute of the component.
Categoryattribute specifies the name of the attribute or event group category when the attribute or event is displayed in a system. Windows. Forms. propertygrid control set to the sorted order mode.
Descriptionattribute specifies the description of attributes or events.
When writing a custom class, all attributes should have get and set methods. If there is no get method, this attribute is not displayed in propertygrid, if the set method is not available, the value of the read-only attribute cannot be set in propertygrid.
Bind the custom class to the propertygrid control.
Use the selectedobject attribute of the propertygrid class for binding
Private void form1_load (Object sender, system. eventargs E)
{
Customer customer = new customer ();
Customer. Name = "James ";
Customer. Email = "zhangsan@sina.com ";
Propertygrid1.selectedobject = customer;
}
Running result:
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/5956/archive/2004/06/30/30223.aspx