By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,564 Members | 1,681 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,564 IT Pros & Developers. It's quick & easy.

Re: iteration (property grid)

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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!
*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #2

P: n/a
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
Jun 27 '08 #3

P: n/a


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!

*** Sent via Developersdex http://www.developersdex.com ***
Jul 1 '08 #4

P: n/a
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
Jul 1 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.