This is a migrated thread and some comments may be shown as answers.

Binding a referenced member to RadGrid while using EntityDataSource control

3 Answers 162 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sundarar Ramasamy
Top achievements
Rank 1
Sundarar Ramasamy asked on 18 Feb 2010, 02:15 AM

Hi,

I got stuck in an issue while trying to bind a reference member (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> 
 
I'm able to populate the dropdownlistbox, but I get the following error, when I try to insert a new record.
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? Do I need two separate EntityDataSource control or just one control is enough?

I use the Telerik RadControls for ASP.NET AJAX Q3 2009 SP2. The environment is as follows:
Windows XP SP3, Visual Studio 2008/.NET Framework 3.5, Visual C#.

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 18 Feb 2010, 10:17 AM
Hi,
 
Please have a look at the forum link below where I have provided a similar solution on how to populate the dropdownlist in the template column based on a foreign key value. You can use a similar approach and use the label  as the control parameter  for the data source control in the EditItemTemplate.
 
 
 
Hope this helps.
 
Thanks,
Princy
0
Saxon
Top achievements
Rank 1
answered on 22 Apr 2016, 08:55 AM
dead link reference
0
Marin Bratanov
Telerik team
answered on 26 Apr 2016, 11:47 AM

Hello Saxon,

I have found the post from Princy in an old database and I am pasting it below. Note that it had been in the old ASP.NET "Classic" forums and that suite is discontinued. As such, the post is very old so I removed the original link as it cannot be restored.

Hi,

I have placed the SqlDataSource in the ItemTemplate along with the Dropdownlist and used an invisible label to pass the key value as the parameter. Please try this and let me know how it goes.

ASPX:

<radG:GridTemplateColumn>
                  <ItemTemplate>
                      <asp:Label ID="Label1" Visible="false" runat="server" Text='<%#Eval("EmployeeID") %>'></asp:Label>
                      <asp:SqlDataSource ID="SqlDataSource2" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
                          runat="server" SelectCommand="SELECT TerritoryID FROM EmployeeTerritories  where EmployeeID = @EmployeeID">
                          <SelectParameters>
                              <asp:ControlParameter ControlID="Label1" PropertyName="Text" Type="String" Name="EmployeeID" />
                          </SelectParameters>
                      </asp:SqlDataSource>
                      <asp:DropDownList ID="DropDownList1" DataTextField="TerritoryID" DataValueField="TerritoryID"
                          DataSourceID="SqlDataSource2" runat="server">
                      </asp:DropDownList>
                  </ItemTemplate>
</radG:GridTemplateColumn>

Thanks,
Princy 

Regards,

Marin Bratanov
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Tags
Grid
Asked by
Sundarar Ramasamy
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Saxon
Top achievements
Rank 1
Marin Bratanov
Telerik team
Share this question
or