views:

541

answers:

2

I have an object similar to what's listed below. When I try to access the properties of the nested business object, I get the error:

A field or property with the name 'Prop2.Property1' was not found on the selected data source.

The gridview is bound to an objectdatasource if that is important.

Is there any way around this?

Sample class:

public class Object1
{
    public string Prop1 { get; set; }
    public Object2 Prop2 { get; set; }
}
A: 

The column name 'Prop2.Property1' does not exist from the data it is retriveing it from.

waqasahmed
+4  A: 

Only immediate properties of an instance can be displayed in a BoundField column.

One must instead use DataBinder.Eval in an itemtemplate to access the nested property instead of assigning it to a boundfield.

Example:

<asp:TemplateField>
    <itemtemplate>
     <p><%#DataBinder.Eval(Container.DataItem, "Prop2.Property1")%></p>
    </itemtemplate>
</asp:TemplateField>

Alternatively, you can create a custom class which inherits BoundField and overrides GetValue to use DataBinder.Eval, as described in this blog post:

http://iridescence.no/post/FixingBoundFieldSupportforCompositeObjects.aspx

Evan