开发者

Combobox selectedvalue

I have a combobox on my form (w开发者_运维百科informs). In the properties I have set the DisplayMember and the ValueMember. DisplayMember = Name and ValueMember = ID. The Combobox is populated with the following objects:

public class MyObj
    {
        public string Name
        {
            get; set;
        }

        public int ID { get; set; }
    }

The Name displays fine in the dropdown(so DisplayMember is working) however, when I do:

mycombobox.SelectedValue it is ALWAYS null.

Does anyone know if I've forgotten to do anything?


Have you set the DataSource property. Also make sure that you have to set them in the correct order -

Set them in the following order -

1. DisplayMember
2. ValueMember
3. DataSource

See this link - http://social.msdn.microsoft.com/Forums/en/winformsdatacontrols/thread/211a46f5-5971-4ea2-a61d-84e389360909

Alternatively you can use the SelectedItem property to get the selected MyObj instance.


try SelectedItem

MyObj obj = (MyObj)mycombobox.SelectedItem;


Has an item been selected? Selected is not always the same as visible. Perhaps you really want to mycombobox.Text.


I have similar mistake. I set DisplayMember and ValueMember, but i set instead of DataSource Items.Insert.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜