Property Grid Control

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.