How can I set the column width of a Property Grid?

Ali Ahmadi picture Ali Ahmadi · Sep 16, 2012 · Viewed 12.7k times · Source

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.

Answer

Nic picture Nic · Jan 23, 2013

I found that the solution of hamed doesn't work reliably. I have solved it by programmatically simulating the user dragging the column splitter. The following code uses reflection to do this:

public static void SetLabelColumnWidth(PropertyGrid grid, int width)
{
    if(grid == null)
        return;

    FieldInfo fi = grid.GetType().GetField("gridView", BindingFlags.Instance | BindingFlags.NonPublic);
    if(fi == null)
        return;

    Control view = fi.GetValue(grid) as Control;
    if(view == null)
        return;

    MethodInfo mi = view.GetType().GetMethod("MoveSplitterTo", BindingFlags.Instance | BindingFlags.NonPublic);
    if(mi == null)
        return;
    mi.Invoke(view, new object[] { width });
}