views:

26

answers:

1

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.

A: 

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.

Teddy