Combobox selectedvalue

I have a summary in my form (winforms). In the properties, I set DisplayMember and ValueMember. DisplayMember = Name and ValueMember = ID. Combobox is populated with the following objects:

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

The name appears in the drop-down list (so that DisplayMember works), however, when I do this:

mycombobox.SelectedValue is ALWAYS null.

Does anyone know if I forgot to do something?

+4
source share
4 answers

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

Install 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 instance of MyObj .

+4
source

try SelectedItem

 MyObj obj = (MyObj)mycombobox.SelectedItem; 
0
source

Is an item selected? The selected one does not always coincide with the visible one. Perhaps you really want mycombobox.Text.

0
source

I have the same error. I install DisplayMember and ValueMember, but I install DataSource Items.Insert instead.

0
source

All Articles