locked
Relational data, Include, BoundField and TemplateField RRS feed

  • Question

  •  

    Hi there,

    I am trying to bind some relational entity data to a GridView.

    I am creating an article list and I have an Article entity that has a relation to an User entity.
    I am using "Include=User" when retrieving the articles.

     

    Now my problem/question is why I can't access the releated properties of the User object from a BoundField in the gridview?

    <asp:BoundField DataField="User.UserName" HeaderText="UserName" />

     

    The error I get from the above is: A field or property with the name 'User.UserName' was not found on the selected data source.

     

     

    But if I convert the field into a TemplateField it works!

    <asp:TemplateField HeaderText="UserName" SortExpression="UserName">

    <ItemTemplate>

    <asp:Label ID="Label1" runat="server" Text='<%# Bind("User.UserName") %>'></asp:Label>

    </ItemTemplate>

    </asp:TemplateField>

     

    I have tried using both an EntityDataSource control and manually by populating the GridView.DataSource.

    I have also tried "Include=User.UserName" but that leads to this error: A specified Include path is not valid. The EntityType 'DataModel.User' does not declare a navigation property with the name 'UserName'

     

    Does anyone have some ideas?
    I can live with using a TemplateField, it would just have been nice if the BoundField worked as well.

     

    Best Regards,
    Ronny

    Wednesday, October 1, 2008 9:38 AM

Answers

  • I believe a BoundField can only specify top level fields and cannot "navigate" to sub properties. To do that, you need to use a template field.

     

    Also, the Include operator only applies to entities and their navigation properties. You do not need to include the specific scalar property (User.Username).

     

    Jeff

    Wednesday, October 1, 2008 4:15 PM

All replies

  • I believe a BoundField can only specify top level fields and cannot "navigate" to sub properties. To do that, you need to use a template field.

     

    Also, the Include operator only applies to entities and their navigation properties. You do not need to include the specific scalar property (User.Username).

     

    Jeff

    Wednesday, October 1, 2008 4:15 PM
  •  

    Thanks for the answer Jeff.

     

    I get what you are saying, that was also my initial "conclusion".

    I think what made me wonder was that my foreign key property was referenced (in a BoundField) using a "sub property" i.e. User.UserID.
    So I asumed that other properties of a sub object could be accessed as well.

     

    But thanks for confirming this.

     

    Regards,

    Ronny

    Wednesday, October 1, 2008 5:17 PM
  • I have the same issue when deployed to IIS only.  Local debugging doesnt give me the field/property wans't found in the datasource.

    <Columns>
      <asp:BoundField HeaderText="Market Line Team" DataField="MarketLineTeam.Description" />
      <asp:BoundField HeaderText="Product Line" DataField="ProductLine.Description" />
                        <asp:BoundField HeaderText="Percentage" DataField="Percent" />
    </Columns>

    Above works fine debugging but fails when deployed to IIS.  Why?
    Saturday, January 5, 2013 10:43 PM