但是为什么会出现这样奇怪的错误呢?
经过调查发现是因为设置了ComboBox的Sorted属性引起的。
重新的测试代码如下:
[复制到剪贴板] |
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DataRowView drv = comboBox1.SelectedItem as DataRowView;
label1.Text = string.Format("Value: {0} == {1} ?", comboBox1.SelectedValue, drv.Row["ID"]);
}
private void frmTest_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Rows.Add(1, "f - 1");
dt.Rows.Add(2, "e - 2");
dt.Rows.Add(3, "d - 3");
dt.Rows.Add(4, "c - 4");
dt.Rows.Add(5, "b - 5");
dt.Rows.Add(6, "a - 6");
comboBox1.DataSource = dt;
comboBox1.ValueMember = "ID";
comboBox1.DisplayMember = "Name";
}