PropertyInfo for a DataRow from its PropertyDescriptor

1.9k views Asked by At

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?

2

There are 2 answers

3
Marc Gravell On BEST ANSWER

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.

0
S. Rojak On

A DataView is built over a DataTable:

DataView viewData = table.DefaultView;

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

(view as ITypedList).GetItemProperties(null)