简体   繁体   中英

ASP.NET entity framework code first foreign key on gridview

I am using this method to retrieve company from trade. How can I display company ID on GridView? This is my code to retrieve.

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

    return trades;
}

I bind to List at code behind... This is my 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>

Instead of using a BoundField you can use a template field:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM