这是我的代码 -
combo.DisplayMember = "Caption";
combo.ValueMember = "PortName";
combo.Items.Add(new { PortName = "port", Caption = "caption" });
//Null reference exception here-
String PortName = combo.SelectedValue.ToString();
我错过了什么?
UPDATE -
//the following line has solved my problem- dynamic item = cmbPortNo.SelectedItem; string PortName = item.PortName;
答案 0 :(得分:0)
也许您只是缺少combo.SelectedIndex = 0;
行
答案 1 :(得分: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;