按索引ID检索DropDownList的值

时间:2011-02-16 20:59:46

标签: c# drop-down-menu

我试图通过指定其indexid来检索下拉列表的值,但我似乎无法找到实现此目的的方法。我也不希望它成为选定的值,基本上我需要浏览列表,然后将其添加到数组中。我可以找到各种方法来获取它的价值,但不是通过索引ID,任何人都知道如何做到这一点?我正在使用c#。

4 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:0)

您是否尝试过lstWhatever.SelectedIndex

由于对象支持IEnumerable,您可以使用foreach进行循环,如果这是您的意图。

答案 2 :(得分:0)

这是你在找什么?

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

comboBox1是你的下拉列表,我假设你在谈论一个Windows窗体项目。

或者,如果您想要我们索引ID:

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

答案 3 :(得分:-2)

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

相关问题