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

Populate Dropdownlist with value from Row Selection on Edit

1 Answer 162 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Colin
Top achievements
Rank 1
Colin asked on 08 Jan 2021, 09:52 PM

I am relatively new to RadGrid and have run into a situation when editing a record in the grid. I am able to get text values in with no issue but when trying to create a dropdown list and set it's selected value to the value from the row selected I get the following error:

 

" 'ddlEmpType' has a SelectedValue which is invalid because it does not exist in the list of items. "

I'm not sure where my code is wrong. Here is a snippet:

 

<EditFormSettings EditFormType="Template">
                                    <FormTemplate>
                                        <table id="Table2" cellspacing="2" cellpadding="1" width="100%" border="0" rules="none"
                                            style="border-collapse: collapse;">
                                            <tr class="EditFormHeader">
                                                <td colspan="2">
                                                    <b>Employee Details</b>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    <table id="Table3" width="450px" border="0" class="module">
                                                        <tr>
                                                            <td>Employee ID:</td>
                                                            <td>
                                                                <asp:TextBox ID="tbEmployeeID" runat="server" Text='<%# Bind("IDNUM") %>'></asp:TextBox>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td>Last Name:</td>
                                                            <td>
                                                                <asp:TextBox ID="tbLastName" runat="server" Text='<%# Bind("LASTNAME") %>'></asp:TextBox>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td>First Name:</td>
                                                            <td>
                                                                <asp:TextBox ID="tbFirstName" runat="server" Text='<%# Bind("FIRSTNAME") %>'></asp:TextBox>
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td>Employee Type:</td>
                                                            <td>
                                                                <asp:DropDownList ID="ddlEmpType" runat="server" SelectedValue='<%# Bind("EMP_TYPE") %>'
                                                                    DataSourceID="SQL_EmpType" AppendDataBoundItems="True">
                                                                    <asp:ListItem Selected="True" Text="Select" Value=""></asp:ListItem>
                                                                </asp:DropDownList>
                                                            </td>
                                                        </tr>                                                        
                                                    </table>
                                                </td>                                
                                            </tr>
                                            <tr>
                                                <td colspan="2"></td>
                                            </tr>
                                            <tr>
                                                <td></td>
                                                <td></td>
                                            </tr>
                                            <tr>
                                                <td align="right" colspan="2">
                                                    <asp:Button ID="btnUpdate" Text="Update" runat="server" CommandName="Update"></asp:Button>&nbsp;
                                                    <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" CommandName="Cancel"></asp:Button>
                                                </td>
                                            </tr>
                                        </table>
                                    </FormTemplate>
                                </EditFormSettings>

 

The datasource is set up as follows:

<asp:SqlDataSource ID="SQL_EmpType" runat="server" ConnectionString="<%$ ConnectionStrings:TECU_WS %>" 
     SelectCommand="SELECT DISTINCT EMP_TYPE FROM vwGetEmployees v WHERE ACTIVESTATUS is not NULL">  
</asp:SqlDataSource>

Hopefully this is enough information to go on.

1 Answer, 1 is accepted

Sort by
0
Doncho
Telerik team
answered on 13 Jan 2021, 04:56 PM

Hi Colin,

It seems there are no values set to the Items of the DropDownList. You can bind the values from the SqlDataSource by using the DataValueField property of the control, and for binding the Text in the items you can use the DataTextField.

The declaration should be similar to the one below:

<asp:DropDownList ID="ddlEmpType" runat="server" SelectedValue='<%# Bind("EMP_TYPE") %>'
    DataSourceID="SQL_EmpType" DataValueField="EMP_TYPE" DataTextField="EMP_TYPE" AppendDataBoundItems="True">
    <asp:ListItem Selected="True" Text="Select" Value=""></asp:ListItem>
</asp:DropDownList>

You may find it useful to check out  Populate (Bind) DropDownList control using SqlDataSource in ASP.Net

Kind regards,
Doncho
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Grid
Asked by
Colin
Top achievements
Rank 1
Answers by
Doncho
Telerik team
Share this question
or