C# - 将列表转储到下拉列表

标签 c# list drop-down-menu

List<String> nameList = new List<String>();
DropDownList ddl = new DropDownList();

列表在这里填充,然后排序:

nameList.Sort();

现在我需要将它放到下拉列表中,这是我遇到问题的地方(使用 foreach):

foreach (string name in nameList){
    ddl.Items.Add(new ListItem(nameList[name].ToString()));
}

没有工作人员 - 有什么建议吗?它给我编译错误:

Error - The best overloaded method match for 'System.Collections.Generic.List<string>.this[int]' has some invalid arguments 

Error - Argument '1': cannot convert from 'string' to 'int'

最佳答案

为什么不像直接将 DDL 绑定(bind)到列表

DropDownList ddl = new DropDownList();
ddl.DataSource = nameList;
ddl.DataBind();

关于C# - 将列表转储到下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/247856/

相关文章:

c# - Jenkins 未使用新的 MSBuild 恢复目标恢复 NuGet 包

c# - WPF Styling 样式中的嵌套元素

c# - 如何在 vs2008 中更改 msi 安装程序包的图标并在为 c# 项目创建包后更改用户界面屏幕

python - 找到所有可能的组合

list - flutter - 如何将 List 中的值向上定位?

r - 从列表中提取相同的索引元素

c# - 在 PRISM 中导航到新 View 时如何传递对象?

html - 使 overflow-y 显示在 <ul> 边框内

asp.net - 用数字填充asp.net下拉列表

php - 下拉值不显示在 codeigniter 的 View 文件中