1

所以这段代码对另一个代码工作得很好ComboBox,但即使我改变了代码中的所有内容,它应该现在拒绝工作 - 不同ComboBox的,不同的来源,但你知道忽略它是相同的情况。

ComboBox事件中的代码Selectionchanged

ComboBox comboBox = (ComboBox)sender;
string id = comboBox.SelectedValue.ToString();

它正在抛出:

Object reference not set to an instance of an object.

与选定值一致。

4

1 回答 1

1

试试下面

ComboBox comboBox = sender as ComboBox;
if(comboBox!=null  && comboBox.SelectedValue != null){ 
      string id = comboBox.SelectedValue.ToString();
}

需要检查相关的其他代码以找出为什么SelectedValue为空。

于 2013-06-09T08:15:35.920 回答