Search code examples
c#.netpropertiespropertygrid

C# .Net 4.5 PropertyGrid: how to hide Properties


The problem is simple(and I hope that this have a simple solution!): I want to hide ( Browsable(false) ) the property "Element" (in my PropertyGrid object) when it's zero.

    public class Question
    {
       ...

      public int Element
      {
        get; set;
      }
    }

Solution

  • What you could do is reuse the DynamicTypeDescriptor class described in my answer to this question here on SO: PropertyGrid Browsable not found for entity framework created property, how to find it?

    like this for example:

    public Form1()
    {
        InitializeComponent();
    
        DynamicTypeDescriptor dt = new DynamicTypeDescriptor(typeof(Question));
    
        Question q = new Question(); // initialize question the way you want    
        if (q.Element == 0)
        {
            dt.RemoveProperty("Element");
        }
        propertyGrid1.SelectedObject = dt.FromComponent(q);
    }