相关文章推荐
悲伤的汉堡包  ·  2018-10-09 ...·  1 年前    · 
儒雅的书包  ·  c++ - How to ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

How to get the PropertyInfo for a DataRow from its PropertyDescriptor.

//pd is a PropertyDescriptor
var propertyinfo = pd.ComponentType.GetProperty(pd.Name);

the above code works fine for a list collection, but it not works while am working with DataTable.

Any idea on this?

What are you later using propertyInfo for? I'm guessing to get the value of the property, but anything else? – Mike Perrenoud Aug 15, 2013 at 13:12

PropertyInfo is the reflection world, where types have explicit CLI properties. DataTable does not belong to that world; it uses the System.ComponentModel flexible property model, via TypeDescriptor, PropertyDescriptor, etc. Basically: there is no property in the CLI sense. PropertyDescriptor can be used (and is used) to describe "properties" in a more flexible, dynamic sense, where the layout is not specified as a type, but is custom-defined, often on-the-fly at runtime.

So no: you can't do this. The question does not make sense; or at least, in the general case it doesn't. There is also "typed datasets", but frankly I strongly recommend staying far far away from them.

Incidentally, you can invent your own pseudo-properties for any type - there are extension points for this (TypeDescriptor, ITypedList, ICustomTypeDescriptor, TypeConverter, etc); but only code that explicitly uses System.ComponentModel will ever see them.

thanks for the detailed answer... i would like to know another thing, how to add validating attributes to a DataColumn.. – Sankarann Aug 15, 2013 at 13:29 @Sankarann technically PropertyDescriptor can describe arbitrary attributes - but: it depends on the validation framework choosing to support PropertyDescriptor. So again: what validation framework? – Marc Gravell Aug 15, 2013 at 19:43

DataView viewData = table.DefaultView;

Since a DataView implements ITypedList, you can get a collection of PropertyDescriptors from it:

(view as ITypedList).GetItemProperties(null)

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.