Windows Develop Bookmark and Share   
 index > Windows Forms General > Display the value of a combobox based on ValueMember
 

Display the value of a combobox based on ValueMember

I need to retrieve information from a COMBOBOX, based on it's ValueMember. I have the following:

combobox1.DisplayMember = "country";
combobox1.ValueMember = "country_id";

How do I retrieve the "country" name, based on it's corresponding "country_id"?

Thanks to all,
Terry
  • Moved byTaylorMichaelLMVPThursday, September 24, 2009 3:51 PMWinForms related (From:Visual C# General)
  •  
tarne  Thursday, September 24, 2009 2:34 PM
I did not try this ... but this should work:

1) Set the selected value (comboBox1.SelectedValue = 22;)

2) Get the text from the combobox (countryName = combobox1.Text;)

Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Thursday, September 24, 2009 3:27 PM
Hello,

You have to two ways, either you handle the underlying collection of the ComboBox by extending it, or you can use the DataSource and do something like this.

Code Snippet
  1. class CustomListItem {
  2. string id;
  3. string value;
  4. public CustomListItem(string id, string value) {
  5. this.id = id;
  6. this.value = value;
  7. }
  8. public string Id {
  9. get { return id; }
  10. set { id = value; }
  11. }
  12. public string Value {
  13. get { return this.value; }
  14. set { this.value = value; }
  15. }
  16. }


Code Snippet
  1. IList<CustomListItem> list = new List<CustomListItem>();
  2. list.Add(new CustomListItem("id1", "Item1"));
  3. list.Add(new CustomListItem("id2", "Item2"));
  4. list.Add(new CustomListItem("id3", "Item3"));
  5. comboBox1.DisplayMember = "Value";
  6. comboBox1.ValueMember = "Id";
  7. comboBox1.DataSource = list;
  8. foreach (CustomListItem item in comboBox1.Items)
  9. {
  10. Console.WriteLine(item.Value);
  11. }


Note, when you're using the DataSource property you can't add items directly to the control i.e. ComboBox.Items.Add
Eyal, Regards.

blog.eyalsh.net
  • Proposed As Answer byEyal-Shilony Thursday, September 24, 2009 6:17 PM
  •  
Eyal-Shilony  Thursday, September 24, 2009 3:49 PM
Hello Terry,

Based on my experience, DeborahK’s solution does work for this case. But the defect is it will change your ComboBox’s current selection and also fire the SelectedIndexChanged event. That will affect the function of your application.

I think you can put a loop to check all the items in DataSource and get the item whose Country_ID is the one you are looking for, then you can get Country name from that item. It won’t affect other part of your application. If you have any problems in doing this, please tell us your DataSource type and we can find a solution for you.

Sincerely,
Kira Qian
Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!
Kira Qian  Friday, September 25, 2009 7:29 AM

You can use google to search for other answers

Custom Search

More Threads

• Extending default ContextMenu
• Treeview flicker with TVS_EX_FADEINOUTEXPANDOS
• Error when deleting images in a PictureBox
• giving a panel focus
• Skinning is xaml?
• line spacing
• Open form inside another form
• Create a taskbar applet like Media Player 9
• BUG in SDK Type.GetProperty is null between OS's Test
• WMS multicast