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

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

1 Answer 20 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Perry
Top achievements
Rank 1
Perry asked on 12 Jul 2019, 12:09 PM

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"
                        AppendDataBoundItems="true"
                        SelectedValue='<%# DataBinder.Eval(Container, "DataItem.SubjectId_FK") %>'>
                    </asp:DropDownList>

 

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.Items.Clear();
//ddlComparisonSubjects.SelectedIndex = -1;
//ddlComparisonSubjects.SelectedValue = null;
//ddlComparisonSubjects.ClearSelection();
ddlComparisonSubjects.DataBind();
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?

1 Answer, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 16 Jul 2019, 08:42 AM
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.

Regards,
Eyup
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.
Tags
Grid
Asked by
Perry
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Share this question
or