views:

16

answers:

1

Hi,

I have a GridView DataBind with entity ClassA's properties that is working fine. I am able to directly bind below properties in ASPX file. ClassA.Id ClassA.Name etc.

But ClassA also have a navigation property to related ClassB. I would like in a the same GridView to display related classB's properties.

I try to bind the following in the GridView but it does not work even if I am able to properly evalute the below value in debug mode (entity performs lazy loading when required). ClassA.classB.Name

How should I proceed ?

+1  A: 

You can achive your goal by a template column with an eval function as below;

<asp:TemplateField HeaderText="Name" SortExpression="Name">
    <ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="TextBox1" runat="server"
                Text='<%# Eval("ClassA.ClassB.Name") %>'></asp:TextBox>
        </EditItemTemplate>
        <asp:Label ID="Label1" runat="server"
            Text='<%# Eval("ClassA.ClassB.Name") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

The downside of this approach is disabling the two-way databinding feature by using the late-bound eval method.

orka
Thanks for your suggestion. It works well but as you said it does not enable 2-way databinding. Do you see any alternative ?
Sylvain
There are some alternatives such as implementing ITypedlist or IBindingList interface for your data sources. But it is not a trivial task. So you should decide if it is worth to create a custom binding provider.
orka