How to enumerate PropertyGrid items?
See the question and my original answer on StackOverflowHere is a piece of code that will retrieve all GridItem objects of a property grid:
public static GridItemCollection GetAllGridEntries(this PropertyGrid grid)
{
if (grid == null)
throw new ArgumentNullException("grid");
var field = grid.GetType().GetField("gridView", BindingFlags.NonPublic | BindingFlags.Instance);
if (field == null)
{
field = grid.GetType().GetField("_gridView", BindingFlags.NonPublic | BindingFlags.Instance);
if (field == null)
return null;
}
var view = field.GetValue(grid);
if (view == null)
return null;
try
{
return (GridItemCollection)view.GetType().InvokeMember("GetAllGridEntries", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, view, null);
}
catch
{
return null;
}
}
Of course, since this is using an undocumented private field of the Property Grid, is not guaranteed to work in the future :-)
Once you have all the GridItems, you can filter them using the GridItem.GridItemType property.