iteration (property grid)

C

csharpula csharp

Hello,

I am trying to iterate over PropertyGrid items on form Load in order to
check the validity of those items.

How can I implement in code this iteration over each property grid
field?
Thank u!
 
M

Marc Gravell

Well, what exactly do you want to do?

PropertyGrid is really at the whim of System.ComponentModel - and
there are various ways of investigating properties; the most common
being TypeDescriptor.GetProperties(). The tricky bit is sub-
properties, since to walk everything you'd need to look at
TypeConverters - it wouldn't be much fun.

Did you see the PropertyGrid/error-provider example I did for you the
other day? This might be an option. Alternatively you can provide your
own custom tab that could make this data more public (since it is tabs
that are asked to provide properties).

Alternatively, you might use IDataErrorInfo directly, since this is
the same validation that drives (as one example) the validation in
DataGridView.

So; what exactly do you want to do?

Marc
 
C

csharpula csharp

Hello,
I have only one level of properties field ,don't have sub fields. So is
there a good solution for that case? All I need it to iterate through
them and to check if the values are valid.
Thanks again!
 
M

Marc Gravell

Well, there are various ways of enumerating the properties, but I'm not
sure how that helps you validate things... you are going to have to find
some fun ways of validating arbitrary properties. That is why I
suggested IDataErrorInfo, as this provides a supported mechanism for an
object to validate *itself* and report any inconsistencies.

For enumerating the properties of an object, the simplest approach is
something like:

foreach(PropertyDescriptor prop in TypeDescriptor.GetProperties(obj))
{
Console.WriteLine("{0}={1}", prop.Name, prop.GetValue(obj));
}

However, note that PropertyGrid is a highly customisable control. For
example, it applies attribute-based filtering to properties (defaults to
[Browsable(true)], and allows individual tabs to shim other members as
properties - for example the "events" tab in VS.
Thus, if you have a complex setup, then to get the same list of
properties that PropertyGrid is using, you would have to do something like:

Attribute[] attribs =
new Attribute[grid.BrowsableAttributes.Count];
grid.BrowsableAttributes.CopyTo(attribs, 0);

foreach (PropertyDescriptor prop in
grid.SelectedTab.GetProperties(grid.SelectedObject, attribs))
{
Console.WriteLine("{0}={1}",
prop.Name, prop.GetValue(grid.SelectedObject));
}

But again - unless you have some metadata-based idea for validing
individual properties, I'm not sure how this is going to help you...

Marc
 
C

csharpula csharp

but in the following code :
Console.WriteLine("{0}={1}",
prop.Name, prop.GetValue(grid.SelectedObject));


I don't have grid.SelectedObject ,I want I am doing it this way :

(prop.DisplayName,prop.Value??) How can I get a value associated with
current property in the iteration ? I can't do it with SelectedObject
because i am going over the values on load and not on click.
Thank u!
 
M

Marc Gravell

The SelectedObject is, confusingly, nothing to do with a selection; it
simply holds the object that we want to display properties for. You have
to have an object to use PropertyGrid...

(not to be confused with SelectedGridItem, which *is* related to a
regular selection).

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top