0

这是我的代码 -

combo.DisplayMember = "Caption";
combo.ValueMember = "PortName";
combo.Items.Add(new { PortName = "port", Caption = "caption"  });

//Null reference exception here-
String PortName = combo.SelectedValue.ToString();

我错过了什么?

更新-

//the following line has solved my problem-
dynamic item = cmbPortNo.SelectedItem;
string PortName = item.PortName;
4

2 回答 2

0

也许你只是错过了combo.SelectedIndex = 0;线路

于 2017-02-24T07:11:24.140 回答
0

您可能缺少 DataSource 属性:

List<CaptionPortCollection> list = new List<CaptionPortCollection>();
list.Add(new CaptionPortCollection() { Caption = "HTTP", Port = 80} );
list.Add(new CaptionPortCollection() { Caption = "HTTPS", Port = 443} );
...

combo.DisplayMember = "Caption";
combo.ValueMember = "Port";
combo.DataSource = list;
于 2017-02-24T07:18:49.040 回答