I am using property grid in my application to display the name and value of the properties of an object.
By default the width of the columns (name and property) are at a ratio of 50:50. and we have an option of sliding the splitter to change this width. I would like to know how this width can be adjusted programmatically so that it can be set at say 25:75.
2019 Answer
Other answers on this page contain adhoc improvements over the course of C# versions and user comments.
I picked the best working solution and created an Extension method.
public static class PropGridExtensions
{
public static void SetLabelColumnWidth(this PropertyGrid grid, int width)
{
FieldInfo fi = grid?.GetType().GetField("gridView", BindingFlags.Instance | BindingFlags.NonPublic);
Control view = fi?.GetValue(grid) as Control;
MethodInfo mi = view?.GetType().GetMethod("MoveSplitterTo", BindingFlags.Instance | BindingFlags.NonPublic);
mi?.Invoke(view, new object[] { width });
}
}
Usage:
In Form_Load() event, call it directly on your property grid like so:
myPropertyGrid.SetLabelColumnWidth(value);
You shouldn't need to call it anywhere else. Call once and enjoy.
Version for Framework 4.0 (I had to use BaseType). Method is used in class inherited from PropertyGrid:
private void SetLabelColumnWidth(int width)
{
FieldInfo fi = this.GetType().BaseType.GetField("gridView", BindingFlags.Instance | BindingFlags.NonPublic);
object view = fi.GetValue(this);
MethodInfo mi = view.GetType().GetMethod("MoveSplitterTo", BindingFlags.Instance | BindingFlags.NonPublic);
mi.Invoke(view, new object[] { width });
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With