Search code examples
c#propertygrid

Expand C# propertygrid on show


i have a question about property grid. when the form is shown i would like a group to be expand rather then collapsed. i have search a lot for that on the web and could not find it yet. any thoughts.


Solution

  • If you want to expand all items in the grid it is fairly simple. The property grid has a method for doing that:

    propertyGrid.ExpandAllGridItems();
    

    If it is a certain group you want to expand you can use this method:

    private static void ExpandGroup(PropertyGrid propertyGrid, string groupName)
    {
        GridItem root = propertyGrid.SelectedGridItem;
        //Get the parent
        while (root.Parent != null)
            root = root.Parent;
    
        if (root != null)
        {
            foreach (GridItem g in root.GridItems)
            {
                if (g.GridItemType == GridItemType.Category && g.Label == groupName)
                {
                    g.Expanded = true;
                    break;
                }
            }
        }
    }