C# - 将列表转储到下拉列表 [英] C# - Dumping a list to a dropdownlist

查看:22
本文介绍了C# - 将列表转储到下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

List is populated here, then sorted:

nameList.Sort();

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

Now I need to drop it into the dropdownlist, which is where I'm having issues (using foreach):

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

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

No workie - any suggestions? It's giving me compile errors:

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.Items.Add(new ListItem(nameList[name].ToString()));

这样:

 ddl.Items.Add(new ListItem(name));

像晚餐一样完成.

这篇关于C# - 将列表转储到下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆