Search code examples
c#winformspropertygrid

PropertyGrid with same childs and different nodes


I want my custom PropertyGrid with student1 and student2 as nodes with "Name,Section,Percentage,School" as childs for both nodes. I tried like this :

class StudentClass
{
    private string name;
    private string section;
    private string percentage;
    private string school;

    [CategoryAttribute("Student1")]
    public string School
    {
        get { return school; }
        set { school = value; }
    }
    [CategoryAttribute("Student1")]
    public string Percentage
    {
        get { return percentage; }
        set { percentage = value; }
    }
    [CategoryAttribute("Student1")]
    public string Section
    {
        get { return section; }
        set { section = value; }
    }
    [CategoryAttribute("Student1")]
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    private string name1;
    [CategoryAttribute("Student2")]
    public string Name1
    {
        get { return name1; }
        set { name1 = value; }
    }
    private string section1;
    [CategoryAttribute("Student2")]
    public string Section1
    {
        get { return section1; }
        set { section1 = value; }
    }
    private string percentage1;
    [CategoryAttribute("Student2")]
    public string Percentage1
    {
        get { return percentage1; }
        set { percentage1 = value; }
    }
    private string school1;
    [CategoryAttribute("Student2")]
    public string School1
    {
        get { return school1; }
        set { school1 = value; }
    }
}

 public Form1()
    {
        InitializeComponent();
        StudentClass sc = new StudentClass();
        propertyGrid1.SelectedObject = sc1;
    }

The Output is as shown below :

enter image description here

Now in the above picture for Student2 instead of "Name1,Section1,Percentage1,School1" I want to display same as student1. But I didn't get the required output. So Kindly help me out of this. I am using C# Winforms in VS2010

And Suggest me how to deny columns resizing i.e., I should not allow user to resize the columns.


Solution

  • You can use the DisplayName attribute:

    private string name1;
    [CategoryAttribute("Student2"), DisplayName("Name")]
    public string Name1
    {
        get { return name1; }
        set { name1 = value; }
    }
    

    But note that if the user puts the property grid into A-Z mode, they'll both end up next to each other with no real way to tell them apart. You may find there's a more appropriate way to represent your data.