错误:异常详细信息:System.ArgumentOutOfRangeException:'dropdownlist1'具有SelectedValue,该ListValue无效,因为它在项列表中不存在。参数名称:value [英] Error: Exception Details: System.ArgumentOutOfRangeException: 'dropdownlist1' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value
问题描述
当数据库是空的从分贝检索数据时进入下拉列表给出了这样的eror
异常详细信息:System.ArgumentOutOfRangeException: '' dropdownlist1 '' 具有的SelectedValue这是无效的,因为它不存在于项目列表中。
参数名称:值
i尝试了这个:
when the database is empty gives this eror when retrieving data from db into dropdownlist.
Exception Details: System.ArgumentOutOfRangeException: ''dropdownlist1'' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
i have tried this one:
if (dt.Rows[rowIndex]["Zone"].ToString() == null || dt.Rows[rowIndex]["Zone"].ToString() == "")
dropdownlist1.SelectedValue = null;
else
dropdownlist1.SelectedValue = dt.Rows[rowIndex]["Zone"].ToString();
但是问题是它在这种情况下需要数据的第一个值,我已经绑定了下拉列表...
如果没有数据,我怎么能省空?
BUT the problem is that it takes in this case the first value of the datable, i have bind the dropdownlist...
HOW CAN I LEAVE IT EMPTY WHEN THERE IS NO DATA??
推荐答案
只需将一个空项添加到下拉列表中就可以解决这个问题了,然后选择它?
类似于:
Would it be a solution for you to just add an empty item to the dropdownlist, and then select it?
something like:
dropdownlist1.Items.Insert(0, "");
dropdownlist1.SelectedIndex(0);
您只需要设置选项如果下拉列表中有任何记录。尝试:
You need to set the option only if there is any record in dropdown. Try:
if (dt.Rows.Count > 0)
{
// Your logic regarding data - setting control values , etc
...
...
dropdownlist1.SelectedValue = dt.Rows[rowIndex]["Zone"].ToString();
...
...
}
这篇关于错误:异常详细信息:System.ArgumentOutOfRangeException:'dropdownlist1'具有SelectedValue,该ListValue无效,因为它在项列表中不存在。参数名称:value的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!