我正在尝试将一个combobox.selectedValue设置为一个有效的字符串,但是当它的nullorempty出错时它会出错。我尝试了以下代码无济于事:
if (string.IsNullOrEmpty(docRelComboBox.SelectedValue.ToString()))
{
document = "other";
}
else
{
document = docRelComboBox.SelectedValue.ToString();
}
组合框是数据绑定的,但理论上它在某些情况下可能是无空的,我需要能够在那些时间传递其他值。任何帮助都会很棒。
答案 0 :(得分:9)
您可能需要:
if ((docRelComboBox.SelectedValue==null) || string.IsNullOrEmpty(docRelComboBox.SelectedValue.ToString()))
由于SelectedValue
本身可能为空。
答案 1 :(得分:0)
ToString()
为空时调用SelectedValue
可能导致错误。我会尝试:
if (docRelComboBox.SelectedValue == null ||
string.IsNullOrEmpty(docRelComboBox.SelectedValue.ToString()))
{
document = "other";
}
else
{
document = docRelComboBox.SelectedValue.ToString();
}
代替。