Using the standard Microsoft .NET 2.0 WinForms PropertyGridControl control, I was looking for a way to set the size of the two columns (one for the labels, one for the values) so that I can control better how the values are displayed.
After some research with ILSpy on the .NET sources, I found a way to use with reflection. The following helper method can be used for this task:
public static void ResizePropertyGridSplitter(
PropertyGrid propertyGrid,
int labelColumnPercentageWidth)
{
var width =
propertyGrid.Width*(labelColumnPercentageWidth/100.0);
// Go up in hierarchy until found real property grid type.
var realType = propertyGrid.GetType();
while (realType!=null && realType != typeof(PropertyGrid))
{
realType = realType.BaseType;
}
var gvf = realType.GetField(@"gridView",
BindingFlags.NonPublic |
BindingFlags.GetField |
BindingFlags.Instance);
var gv = gvf.GetValue(propertyGrid);
var mtf = gv.GetType().GetMethod(@"MoveSplitterTo",
BindingFlags.NonPublic |
BindingFlags.InvokeMethod |
BindingFlags.Instance);
mtf.Invoke(gv, new object[] { (int)width });
}
Call the function and pass your PropertyGrid instance and width values between e.g. 10 and 90 to resize the first column to the given percentage value.
E.g.:
ResizePropertyGridSplitter( myPropGrid, 75 );
If you specify a too small or too large value, the PropertyGrid seems to use the minimum/maximum possible, based on your passed value.