ComboBox - set selected index

Hi,

I have populated a combobox with a dataadapter and set the DisplayMember and ValueMember as required. So, the conents are essentially:

1 Apple
2 Orange
3 Banana

(Obviously, only the fruit names are visible in the combo)

My problem is, that if I want to display a form with the combobox and set the selected index to the item which has fruit if # 2 (Orange). How do I determine which one I want. Do I have to loop over the contents and check the ValueMember value for item

I suppose I could populate the combo without an adapter, but can I add both a valuemember and a displaymember that way

Thanks,

CW




Answer this question

ComboBox - set selected index

  • spymaster

    Boy...that was easy. Thanks a lot!

  • Shinyzhu

    You can use the SelectedValue property to pick the item you want.


  • Remco van Diesen

    I actually do quite similar but whatever I set SelectedValue to, it does not show the right preselection...

    I have an DictionaryEntry[] arrEntries that holds
    key value
    0 Mr
    1 Mrs
    2 Company

    I assign
    ComboBox cb = new ComboBox ();
    cb.DataSource = arrEntries;
    cb.DisplayMember = "Value";
    cb.ValueMember = "Key";

    Now whatever I assign
    cb.SelectedValue to, it won't show up the right preselection.

    I can read the correct selected value from the control though.

    What am I missing


  • Peruri

    Glad to help, sometimes the sheer amount of functions and properties in the framework can be overwhelming when first learning.


  • ComboBox - set selected index