Expand C# propertygrid on show

guy picture guy · Nov 3, 2010 · Viewed 13k times · Source

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.

Answer

Simon Fischer picture Simon Fischer · Nov 3, 2010

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;
            }
        }
    }
}