atrljoe
atrljoe

Reputation: 8151

Retrieve Value of DropDownList by Index ID

I am trying to retrieve the value of a dropdown list by specifying its indexid but I cant seem to find a way to accomplish this. I dont want it to be the selected value either, basically I need to go through the list, and then add it to an array. I can find all kinds of ways to get it based on the value but not by the index id anyone know how to do this? Im using c#.

Upvotes: 1

Views: 9473

Answers (4)

Benoit Drapeau
Benoit Drapeau

Reputation: 624

string valueAtIndex = myComboBox.Items[SomeIndexValue].Value;

Upvotes: 8

Radu Caprescu
Radu Caprescu

Reputation: 993

Is this what you are looking for?

 List<String> theList = new List<string>();
        foreach (var item in comboBox1.Items)
        {
            theList.Add(item.ToString());
        }

Where comboBox1 is your dropdown list and i assumed you are talking about a windows forms project.

Or, if you want to us Index Id:

List<string> theList = new List<string>();
            for (int i = 0; i < comboBox1.Items.Count; i++)
            {
                theList.Add(comboBox1.Items[i].ToString());
            }

Upvotes: 0

clifgriffin
clifgriffin

Reputation: 2053

Have you tried lstWhatever.SelectedIndex?

Since the object supports IEnumerable, you can use foreach to loop through, if that is your intention.

Upvotes: 0

novacara
novacara

Reputation: 2257

String valueAtIndex = myComboBox.Items[myComboBox.SelectedIndex].ToString();

Upvotes: -2

Related Questions