2011-11-22 97 views
1

我正在使用這種方法從貿易中檢索公司。我如何在GridView上顯示公司ID?這是我的代碼來檢索。ASP.NET實體框架代碼gridview上的第一個外鍵

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

    return trades; 
} 

我綁定在後面的代碼列表...這是我的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> 
+0

你能展示標記爲您的Gridview?你如何將這個列表綁定到你的GridView? –

+0

完成........... – user2514963

回答

1

而不是使用綁定列,您可以使用一個模板字段:

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