views:

867

answers:

1

I'm new to Entity Framework and I got stuck in an issue while trying to bind a navigation property (foreign key reference) to a dropdownlist. I have Telerik RadGrid control which gets the data using a EntityDataSource control. Here is the model description:

Applications: AppId, AppName, ServerId
Servers: ServerId, ServerName

The Applicaitons.ServerId is a foreign key reference to Servers.ServerId. The RadGrid lists the applications and allows the user to insert/update/delete an application. I want to show the server names as a dropdownlist in edit mode which I'm not able to. . Here is my aspx code:

<telerik:RadGrid ID="gridApplications" runat="server" Skin="Sunset" 
    AllowAutomaticInserts="True" AllowAutomaticDeletes="True"
    AllowPaging="True" AllowAutomaticUpdates="True" 
    AutoGenerateColumns="False" OnItemCreated="gridApplications_ItemCreated" 
    DataSourceID="applicationsEntityDataSource" Width="50%" 
    OnItemInserted="gridApplications_ItemInserted" 
    OnItemUpdated="gridApplications_ItemUpdated" 
    OnItemDeleted="gridApplications_ItemDeleted" GridLines="None">
    <MasterTableView CommandItemDisplay="Top" AutoGenerateColumns="False" DataKeyNames="AppId" DataSourceID="applicationsEntityDataSource">
        <RowIndicatorColumn>
            <HeaderStyle Width="20px" />
        </RowIndicatorColumn>
        <ExpandCollapseColumn>
            <HeaderStyle Width="20px" />
        </ExpandCollapseColumn>
        <Columns>
            <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn" HeaderText="Edit" ItemStyle-Width="10%">
            </telerik:GridEditCommandColumn>
            <telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="DeleteColumn" ConfirmText="Are you sure you want to delete this application?" ConfirmTitle="Confirm Delete" ConfirmDialogType="Classic" ItemStyle-Width="10%" HeaderText="Delete">
            </telerik:GridButtonColumn>
            <telerik:GridBoundColumn DataField="AppId" UniqueName="AppId" Visible="false" HeaderText="Application Id" ReadOnly="true">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="AppName" UniqueName="AppName" HeaderText="Application Name" MaxLength="30" ItemStyle-Width="40%">
            </telerik:GridBoundColumn>
            <telerik:GridTemplateColumn DataField="ServerId" UniqueName="ServerId" HeaderText="Server Hosted" EditFormColumnIndex="1">
                <EditItemTemplate>
                    <asp:DropDownList ID="ddlServerHosted" runat="server" DataTextField="Servers.ServerName" DataValueField="ServerId" Width="40%">
                    </asp:DropDownList>
                </EditItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
        <EditFormSettings ColumnNumber="2" CaptionDataField="AppId" InsertCaption="Insert New Application" EditFormType="AutoGenerated">
            <EditColumn InsertText="Insert record" EditText="Edit application id #:" EditFormColumnIndex="0" UpdateText="Application updated" UniqueName="InsertCommandColumn1" CancelText="Cancel insert" ButtonType="ImageButton"></EditColumn>
            <FormTableItemStyle Wrap="false" />
            <FormTableStyle GridLines="Horizontal" CellPadding="2" CellSpacing="0" Height="110px" Width="110px" />
            <FormTableAlternatingItemStyle Wrap="false" />
            <FormStyle Width="100%" BackColor="#EEF2EA" />
            <FormTableButtonRowStyle HorizontalAlign="Right" />
        </EditFormSettings>
    </MasterTableView>
</telerik:RadGrid>
<asp:EntityDataSource ID="applicationsEntityDataSource" runat="server" 
    ConnectionString="name=AnalyticsEntities" EnableDelete="True" 
    EntityTypeFilter="Applications" EnableInsert="True" EnableUpdate="True" EntitySetName="Applications" 
    DefaultContainerName="AnalyticsEntities" Include="Servers">
</asp:EntityDataSource>

I tried another approach where I replaced the GridTemplateColumn with the following code

<telerik:RadComboBox ID="RadComboBox1" DataSourceID="serversEntityDataSource" DataTextField="ServerName" DataValueField="ServerId" AppendDataBoundItems="true" runat="server" >
<Items>
    <telerik:RadComboBoxItem />
</Items>

and using a separate EntityDataSource control as follows:

    <asp:EntityDataSource ID="serversEntityDataSource" runat="server" 
    ConnectionString="name=AnalyticsEntities" EnableDelete="True" 
    EntityTypeFilter="Servers" EnableInsert="True" EnableUpdate="True" EntitySetName="Servers" 
    DefaultContainerName="AnalyticsEntities">
</asp:EntityDataSource>

but, I get the following error.

Application cannot be inserted. Reason: Entities in 'AnalyticsEntities.Applications' participate in the 'FK_Servers_Applications' relationship. 0 related 'Servers' were found. 1 'Servers' is expected. 

My question is, how do you bind the navigation property and load the values in the DropDownList/RadComboBox control?

A: 

I believe you bind your dropdownlist to that second datasource instead of the grid's datasource (as you did in the second example). Then you set the SelectedValue property on the dropdown/combobox using the <%# BIND("ServerId") %> syntax within the template.

Chad