请教C# winform 中comboBox的问题
我建了一个表单,还有一个Northwind的Dataset。
Form中有一个comboBox,同数据源相连(Category表)。其中DisplayMember=CategoryName, ValueMember=CategoryID。
还有三个TextBox, 每一个显示选中的值。程序如下:
private void categoriesComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
this.textBox1.Text = this.categoriesComboBox.SelectedValue.ToString();
this.textBox2.Text = this.categoriesComboBox.Text;
this.textBox3.Text = this.categoriesComboBox.SelectedIndex.ToString();
}
第一个显示的是CategoryID,第二个显示的是CategoryName,第三个显示的是索引值。
运行没问题,但是在关闭时出错(在第一个CategoryID),信息是:
Object reference not set to an instance of an object
请问这是什么原因,怎么解决?多谢!
另外显示名称时,用this.textBox2.Text = this.categoriesComboBox.SelectedText;
也出错,用SelectedItem也一直没有成功。怎么办好呢?