PROGRAMMATICALLY STORING A LIST INTO MY COMBOBOX'S ITEM COLLECTION

Chintoo Semi

Total Post:135

Points:947
Posted by  Chintoo Semi
C# 
C#
 947  View(s)
Ratings:
Rate this:

I am working on a Windows Form right now, specifically, a combo box.

I have two List<string>. Let's say the first List are names, and the second List are ages.

How does one store the entire first List into the collection, so I can use the comboBox to access them?

Upon clicking on an item in this collection, for example, the fifth name, how do I get the index of this item?

So far, I have created my form, and laid the skeleton of the win forms application, but the part of storing the list into the collection is stopping me from moving forward 

 thank you.

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: Programmatically storing a list into my combobox's item collection

    Hi Chintoo,


    For the list of strings it can be done like this

    comboBox.DataSource = myList;

    If you don't want to tie your list in Datasource, still one line

    comboBox.DataSource = myList.ToArray().Clone();

    Now, if this is list of Strings you can get the whole string

    string s = (string)comboBox.SelectedItem; // vs comboBox.SelectedIndex

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!