Click here to Skip to main content
15,885,885 members
Articles / Desktop Programming / Windows Forms

Show Properties of a Class on a PropertyGrid

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
20 Oct 2011CPOL 26.3K   3  
How to show properties of a class on a PropertyGrid

When developing user controls, usually we use Booleans, Integers, Strings, etc. as data types of the attributes of the control. But sometimes, we have to use structures or other classes as attributes. When we use those, we should be able to change or browse the attributes of those structures or classes.

Open Visual Studio IDE and create a new Windows Forms Application type project.

Add a new class and name it MyCustomClass. And define two properties: an integer and a string type.

C#
public class MyCustomClass {

    public int MyIntProperty { get; set; }
    public string MyStringProperty { get; set; }

    public override string ToString() {
        return "...";
    }
}

**Please note that I have overridden the ToString method. Because this is what will be shown on the property grid when the properties are collapsed.

Now add a new user control to the project and name it MyCutomUsercontrol. And create three properties: an integer, string, and MyCustomClass. And use the [TypeConverter(typeof(ExpandableObjectConverter))] attribute on the third property. The syntax should be:

C#
public partial class MyCutomUsercontrol : UserControl {
    public MyCutomUsercontrol() {
        InitializeComponent();
    }

    private MyCustomClass _MyCustomClass = new MyCustomClass();

    public int Property1 { get; set; }
    public string Property2 { get; set; }

    [TypeConverter(typeof(ExpandableObjectConverter))]
    [EditorBrowsable(EditorBrowsableState.Always)]
    public MyCustomClass Property3 {
        get {
            return _MyCustomClass;
        }
        set {
            _MyCustomClass = value;
        }
    }
}

Now build the solution. And add it to a Windows Form. And on the property grid, you can see your custom control's properties.

271589/screen_1_thumb_1_.png

License

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


Written By
Technical Lead Air Liquide Industrial Services (Singapore)
Singapore Singapore
My passion lies in building business intelligence and data-based solutions, writing about things I work with and talking about it. New technologies relevant to my line of work interest me and I am often seen playing with early releases of such technologies.

My current role involves architecting and building a variety of data solutions, providing database maintenance and administration support, building the organization’s data practice, and training and mentoring peers.

My aspiration over the next several years is to achieve higher competency and recognition in the field of Data Analytics and move into a career of data science.


Specialities: SQL Server, T-SQL Development, SQL Server Administration, SSRS, SSIS, C#, ASP.Net, Crystal Reports

Comments and Discussions

 
-- There are no messages in this forum --