471,305 Members | 1,135 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,305 software developers and data experts.

Re: iteration (property grid)

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

So; what exactly do you want to do?

Jun 27 '08 #1
4 1804
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!
*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #2
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))
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...

Jun 27 '08 #3

but in the following code :
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!

*** Sent via Developersdex http://www.developersdex.com ***
Jul 1 '08 #4
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).

Jul 1 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

35 posts views Thread by Raymond Hettinger | last post: by
reply views Thread by Andreas Poller | last post: by
3 posts views Thread by Steve Teeples | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.