ASP.NET实体框架代码第一个是gridview上的外键

时间:2011-11-22 15:36:02

标签: asp.net entity-framework-4.1

我正在使用此方法从交易中检索公司。如何在GridView上显示公司ID?这是我要检索的代码。

public List<Trade> getTrade()
{
    List<Trade> trades=dbContext.trades.Include("tradeCompany")
      .OrderBy(t => t.tradeDate).ToList();

    return trades;
}

我在代码后面绑定到List ...这是我的gridview

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    BackColor="White" OnRowDataBound="sharesGridView_RowDataBound" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" 
    CellPadding="3" GridLines="Vertical">
        <AlternatingRowStyle BackColor="#DCDCDC" />
        <Columns>
            <asp:BoundField DataField="tradeDate" DataFormatString="{0:MMMM d, yyyy}" 
                HeaderText="Date" HtmlEncode="False" SortExpression="tradeDate" />
            <asp:BoundField DataField="type" HeaderText="Type" SortExpression="type" />
            <asp:BoundField HeaderText="Company"  />
            <asp:BoundField DataField="tradePrice" HeaderText="Price" 
                SortExpression="tradePrice" />
            <asp:BoundField DataField="tradeQuantity" HeaderText="Quantity" 
                SortExpression="tradeQuantity" />
        </Columns>
        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
        <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
        <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#0000A9" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#000065" />
    </asp:GridView>

1 个答案:

答案 0 :(得分:1)

您可以使用模板字段代替使用BoundField:

<asp:TemplateField>
    <itemtemplate>
        <p><%#DataBinder.Eval(Container.DataItem, "Company.Id")%></p>
    </itemtemplate>
</asp:TemplateField>
相关问题