C# - 倾倒列表一个DropDownList [英] C# - Dumping a list to a dropdownlist

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

问题描述

 列表<串GT; NAMELIST =新的List<串GT;(); 
DropDownList的DDL =新的DropDownList();



列表在这里居住,然后进行排序:

  nameList.Sort(); 

现在我需要把它拖放到DropDownList的,这就是我遇到的问题(使用的foreach ):在名称列表

 的foreach(字符串名称){
ddl.Items.Add(新的ListItem(名称列表[名]的ToString()));
}

没有workie - 有什么建议?它给我的编译错误:

 错误 - System.Collections.Generic.List<的最佳重载的方法匹配;串取代。这个[INT]'有一些无效参数

错误 - 参数1:不能从转换'字符串'到'廉政'


解决方案

替换这样的:

  ddl.Items.Add(新的ListItem(名称列表[名]的ToString())); 



与此:

  ddl.Items.Add(新列表项(名)); 



做过类似晚餐。


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

List is populated here, then sorted:

nameList.Sort();

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'

解决方案

Replace this:

 ddl.Items.Add(new ListItem(nameList[name].ToString()));

with this:

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

Done like dinner.

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

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