Jesse
Jesse

Reputation: 2053

Entity Foreign Keys in GridViews

Has anyone been able to find a clean solution to display a foreign key value in a GridView using an EntityDataSource?

For example, my Employees table has a (FK)JobTitleID and I want the Employees GridView to display a Job Title column.

Upvotes: 1

Views: 2357

Answers (2)

Greg
Greg

Reputation: 51

I tried this and it didn't work directly as advertised. You also need to ensure that the related tables are referenced in the Include property of the EntityDataSource. I couldn't see any way to control this in the EntityDataSorce 'Configure Data Source' wizard, so I entered it by hand in the HTML.

More info here in the MSDN documentation.

PS. I've read elsewhere that you have to use Eval but I seem to be able to use Eval or Bind interchangeably.

Upvotes: 2

Thea
Thea

Reputation: 8067

You can use bind the value to an TemplateField. Inside you can use whatever type of Bind control that is suitable for you type of data. Here is an example:

<asp:GridView ID="gvEmployees" runat="server" AllowSorting="true" AutoGenerateColumns="false" DataKeyNames="ID" DataSourceID="godsCourses">
    <Columns>
        <asp:TemplateField HeaderText="Job Title">
            <ItemTemplate>
                <asp:Literal ID="hlProgram" runat="server" Text='<%# Bind("JobTitle.Name") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

I have used JobTitle.Name supposing that the JobTitleID is connected to JobTitle entity.

Upvotes: 1

Related Questions