class A
{
int ID;
B ClassB;
}
class B
{
string Title;
}
Using the above mock classes as examples – I have a dynamically created GridView. To this I bind (add to the Columns collection) X number of BoundFields and then assign a collection to the DataSource, however, my sub objects are not binding.
The DataField property of the BoundFields is set to “ClassB.Title” and the DataSource is a collection of objects of type A but it keeps throwing the exception “A field or property with the name ‘ClassB.Title’ was not found on the selected data source. “
Any thoughts why?
Cheers
This is because BoundField uses reflection to evaluate object properties – obviously, class A does not have a property called
ClassB.Titleand hence the issue.You can use template field to work around this issue. For example,
Note that instead of using typical template-field markup such as below, you may use strongly typed expressions such as
Note the type-casting of data-item to your specific class (
A).EDIT: yet another alternative is to build your own
BoundFieldthat can do nested property lookup – for example: