Brown Love
Brown Love

Reputation: 1923

How to clear all the items from a PropertyGrid

Is there a method that can clear the property grid of all its items? Or is there another way I can do this ?

Upvotes: 2

Views: 2722

Answers (2)

Jazimov
Jazimov

Reputation: 13282

The way that worked for me is this:

MyPropertyGrid.SelectedObject = new Object();
MyPropertyGrid.Update();

Noticed that the property grid is set not to a custom object, but to an instance of the type System.Object. Code that needs to inspect the selected object can determine if the property grid is "empty" by testing whether MyPropertyGrid.SelectedObjects returns an array of one element and that MyPropertyGrid.SelectedObjects[0].GetType() == typeof(System.Object).

Upvotes: 0

Steve Danner
Steve Danner

Reputation: 22148

If we can assume that the object you are using to populate the PropertyGrid has "clear" default values, the easiest thing to do is to just new up a new object to put there.

MyPropertyGrid.SelectedObject = new MyObject();

Upvotes: 2

Related Questions