ASP Drop Down Does Not Work for Insert New Record in RadGrid

2 posts, 0 answers
  1. Perry
    Perry avatar
    62 posts
    Member since:
    Jul 2015

    Posted 12 Jul 2019 Link to this post

    I have a Telerik RadGrid using an ascx web form user control for editing and adding new records to the grid.

    On the web form control I have DropDown setup with this 

    <asp:DropDownList ID="ddlComparisonSubjects" runat="server"
                            DataTextField="SubjectName" DataValueField="SubjectId_FK"
                            SelectedValue='<%# DataBinder.Eval(Container, "DataItem.SubjectId_FK") %>'>


    And My C# code is this


    var SubjectNames = db.T_Subjects.Where(a => a.CaseId_FK == intCaseId).Distinct().ToList();
    ddlComparisonSubjects.DataTextField = "SubjectName";
    ddlComparisonSubjects.DataValueField = "SubJectId";
    ddlComparisonSubjects.DataSource = SubjectNames;
    //ddlComparisonSubjects.SelectedIndex = -1;
    //ddlComparisonSubjects.SelectedValue = null;
    ddlComparisonSubjects.Items.Insert(0, "--Select--");


    The code lines commented out were added while troubleshooting this issue.  They did not solve the problem so I commented them out.  I have left them so you can see what I have tried to do to fix the problem.

    Everything works fine if I am editing an existing record. However when I try to add a new record I get this error message:

    'ddlComparisonSubjects' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value

    I know the Parameter names are correct because the drop down works in edit mode.

    So why do these drop downs work in edit mode but not in insert mode and how can I fix it?

  2. Eyup
    Eyup avatar
    3953 posts

    Posted 16 Jul 2019 Link to this post

    Hi Perry,

    The SubjectId_FK value while creating new record should correspond to some value in the dropdownlist options. Try changing the Value of the item with Text "--Select--" to DataValue.Null or String.Empty.

    Alternatively, you can include a ternary if statement in the SelectedValue property or a null coalescing operator, similar to this: 
    <asp:DropDownList ... SelectedValue='<%# DataBinder.Eval(Container, "DataItem.SubjectId_FK") ?? "MyDefaultValue"  %>'

    I hope this will prove helpful.

    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top