如何在实体框架中绑定网格视图一对一的关系

时间:2013-05-16 04:58:27

标签: c# gridview entity-framework-4

我想知道如何使用实体框架在网格视图中显示数据。我的表结构是

StudentId StudentName CourseId          
1       SS              1
2       AA              2


CourseId CourseName
1           English
2           Mathematics

目前我可以在网格视图中显示学生ID和姓名,但不能显示CourseName。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataSourceID="EntityDataSource1" 
            Width="527px">
            <Columns>
                <asp:BoundField DataField="StudentId" HeaderText="Model" ReadOnly="True" 
                    SortExpression="StudentId" />
                <asp:BoundField DataField="StudentName" HeaderText="Costs" ReadOnly="True" 
                    SortExpression="StudentName" />
            </Columns>
        </asp:GridView>
        <asp:EntityDataSource ID="EntityDataSource1" runat="server" 
            ConnectionString="name=CarContext" DefaultContainerName="CarContext" 
            EnableFlattening="False" EntitySetName="Cars" Select="it.[StudentId], it.[StudentName]">
        </asp:EntityDataSource>

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataSourceID="EntityDataSource1" 
            Width="527px">
            <Columns>
                <asp:BoundField DataField="StudentId" HeaderText="Model" ReadOnly="True" 
                    SortExpression="StudentId" />
                <asp:BoundField DataField="StudentName" HeaderText="Costs" ReadOnly="True" 
                    SortExpression="StudentName" />
                <asp:BoundField DataField="CourseName" HeaderText="Course" ReadOnly="True" 
                    SortExpression="CourseName" />
            </Columns>
        </asp:GridView>
        <asp:EntityDataSource ID="EntityDataSource1" runat="server" 
            ConnectionString="name=CarContext" DefaultContainerName="CarContext" 
            EnableFlattening="False" EntitySetName="Cars" Select="it.[StudentId], it.[StudentName],it.[CourseName]">
        </asp:EntityDataSource>

并编辑您的EntityDataSource1的查询,将CourseName包含在您的查询中