2

我有一個EntityDataSource映射到包含兩個導航屬性(Building1,Room1)的實體Resident。我將GridView設置爲使用此EntityDataSource,並將EntityDataSource Include屬性設置爲Building1,Room1,以便它包含這些導航屬性並將這些列添加到GridView。當我運行應用程序而不是顯示關聯的導航屬性時,它顯示了這一點:webHousingAdmin.Building 如何獲取它以顯示實際值? 代碼看起來像這樣的GridView:使用EntityDataSource在GridView上顯示導航屬性?

 <asp:TemplateField> 
      <ItemTemplate> 
       <asp:Label ID="lbl1" runat="server" Text='<%# Bind("Building1") %>' /> 
      </ItemTemplate> 
     </asp:TemplateField> 

我已經得到它,以顯示使用下面的代碼的實際值:

  <asp:TemplateField HeaderText="Building"> 
      <ItemTemplate> 
       <asp:Label ID="lblBuilding" Text='<%# Bind("Building1.building_name") %>' runat="server" /> 
      </ItemTemplate> 
     </asp:TemplateField> 

但有一個簡單的方法來做到這一點?這隻會顯示文字,不會讓我編輯它......如果我可以將它作爲一個理想的界限來執行。

+1

它看起來像你想的財產號樓,房間1等直接綁定到GridView中的一列。由於這些是類而不是簡單的標量屬性''.ToString()'被調用,它只返回類名(如果你沒有覆蓋它)。但這只是一個猜測,你需要展示代碼來獲得有根據的答案。 – Slauma 2011-04-04 14:51:49

+0

我已經添加了上面的代碼。 – davemackey 2011-04-04 15:02:06

回答

2

要獲得您的標籤有意義的事情,你可以你的Building類的標量屬性綁定到Label ...

<asp:Label ID="lbl1" runat="server" Text='<%# Bind("Building1.Name") %>' /> 

...或者你可以覆蓋類的ToString() ...

public class Building 
{ 
    public string Name { get; set; } 
    public string AnotherText { get; set; } 

    public override string ToString() 
    { 
     return string.Concat(Name, ", ", AnotherText); // or whatever you like 
    } 
} 

如果你綁定屬性,它是一類綁定引擎會調用ToString()電網 - 這僅返回類的全名(名字空間點類名),如果你不重寫ToString。這就解釋了爲什麼你在你的例子中只看到webHousingAdmin.Building

編輯

沒有真正涉及到這樣一個問題:但是,如果你嘗試與Bind(不僅Eval)綁定導航屬性的數據源和電網之間的雙向通信可能遇到的問題。可能它不會工作。請參閱此相關的問題和答案:

Columns of two related database tables in one ASP.NET GridView with EntityDataSource

0

這被問到年前,但我發現尋找同樣的前面回答。這對我來說很有用。在ASPX這是既定模式下完整的GridView:

<asp:EntityDataSource ID="dsResidents" runat="server" 
     ConnectionString="name=connString" 
     DefaultContainerName="dbContext" 
     EntitySetName="Residents" 
     Include="Building" 
     EnableUpdate="true" /> 

    <asp:GridView ID="ResidentGrid" runat="server" 
     DataSourceID="dsResidents" 
     OnRowUpdating="ResidentsGrid_RowUpdating" 
     AutoGenerateColumns="False"> 
     <Columns> 
      <asp:CommandField ShowEditButton="True" /> 
      <asp:TemplateField Visible="false"> 
       <ItemTemplate> 
        <asp:Label ID="lblID" 
         Text='<%# Eval("residentId") %>' 
         runat="server" /> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="Name"> 
       <ItemTemplate> 
        <asp:Label ID="lblName" 
         Text='<%# Eval("residentName") %>' 
         runat="server" /> 
       </ItemTemplate> 
      <asp:TemplateField HeaderText="CountryCode"> 
       <ItemTemplate> 
        <asp:Label ID="lblCountryCode" 
         Text='<%# Eval("Building.number") %>' 
         runat="server" /> 
       </ItemTemplate> 
       <EditItemTemplate> 
        <asp:EntityDataSource ID="dsBuildings" runat="server" 
         ConnectionString="name=connString" 
         DefaultContainerName="dbContext" 
         EntitySetName="Buildings" /> 

        <asp:DropDownList ID="ddlBuilding" 
         DataSourceID="dsBuildings" 
         DataTextField="number" 
         DataValueField="id" 
         SelectedValue='<%# Eval("id") %>' <!-- I am not sure about this line --> 
         runat="server" /> 
       </EditItemTemplate> 
      </asp:TemplateField> 
     </Columns> 
    </asp:GridView> 

請注意,如果您使用嚮導,默認情況下創建的實體模型,ConnectionString中名稱相同的DefaultContainerName。在支持類添加事件處理程序的更新網格行(OnRowUpdating):

protected void ResidentsGrid_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 
    // This is for retrieving the entity object for the resident selected 
    Label idLabel = (Label)ResidentsGrid.Rows[e.RowIndex].Cells[1].FindControl("lblID"); 
    int residentId = int.Parse(idLabel.Text); 

    // And this is for the building selected in the dropdown 
    DropDownList ddl = (DropDownList)ResidentsGrid.Rows[e.RowIndex].Cells[4].FindControl("ddlBuilding"); 
    // I would like to say that the cell index is the column position but as I know it isn't 

    using (dbContext ctx = new dbContext()) 
    { 
     Resident resident = ctx.Residents 
      .Where(res => res.residentId == residentId).First(); 

     Building selectedBuilding = ctx.Buildings 
      .Where(bld => bld.id == ddl.SelectedItem.Value).First(); 

     resident.Building = selectedBuilding; 

     ctx.SaveChanges(); 
    } 
}