我正在尝试为一个字符串设置一个combobox.selectedValue,它可以工作,但当它为空时,它会出错。我已经尝试了以下代码,但都没有用:
if (string.IsNullOrEmpty(docRelComboBox.SelectedValue.ToString()))
{
document = "other";
}
else
{
document = docRelComboBox.SelectedValue.ToString();
}combobox是数据绑定的,但理论上它在某些情况下可能是空的,我需要能够在这些时候传递其他值。任何帮助都是最好的。
发布于 2011-05-07 04:13:17
您可能需要:
if ((docRelComboBox.SelectedValue==null) || string.IsNullOrEmpty(docRelComboBox.SelectedValue.ToString())) 因为SelectedValue本身可能为空。
发布于 2011-05-07 04:14:16
当SelectedValue为null时调用ToString()可能是导致错误的原因。我会尝试:
if (docRelComboBox.SelectedValue == null ||
string.IsNullOrEmpty(docRelComboBox.SelectedValue.ToString()))
{
document = "other";
}
else
{
document = docRelComboBox.SelectedValue.ToString();
}而不是。
https://stackoverflow.com/questions/5916508
复制相似问题