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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/247856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 19:46:44  来源:igfitidea点击:

C# - Dumping a list to a dropdownlist

c#listdrop-down-menu

提问by scrot

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):

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'

采纳答案by Mike Burton

Replace this:

替换这个:

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

with this:

有了这个:

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

Done like dinner.

像晚餐一样完成。

回答by Marcus King

Why not just bind the DDL directly to the List like

为什么不直接将 DDL 绑定到 List 之类的

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

回答by Nicholas Mancuso

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

Is your problem.

是你的问题。

it should look more like

它应该看起来更像

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

But I actually like Marcus' suggestion a little better.

但实际上我更喜欢 Marcus 的建议。

回答by wprl

That would be because List is not indexed by string (name) but by ints.

那是因为 List 不是按字符串(名称)而是按整数索引的。

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

Will fix that.

会解决这个问题。

回答by ema

You get that error because the collection nameListis a Listso you must access it using an index not a string (you use name).

您会收到该错误,因为该集合nameList是 a,List因此您必须使用索引而不是字符串(您使用名称)来访问它。

So you can write:

所以你可以写:

foreach (string name in nameList){
    ddl.Items.Add(name);
}

BTW the best way to do this is:

顺便说一句,最好的方法是:

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

回答by Tom

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

Doesn't work if it's a SharePoint list - Error: Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource. Decided to chime in, in case any SharePoint developers thought this was for an SPList instead of List<string>, as was written above.

如果它是 SharePoint 列表,则不起作用 - 错误:数据源类型无效。它必须是 IListSource、IEnumerable 或 IDataSource。决定加入,以防任何 SharePoint 开发人员认为这是针对 SPList 而不是 List<string>,如上所述。

There is a way to bind to an SPList, but you'd use an SPListItemCollection, or go one better and use an SPDataSource. For the SharePoint developers, see this blog by Chris O' Brien.

有一种方法可以绑定到 SPList,但您可以使用 SPListItemCollection,或者更好地使用 SPDataSource。对于 SharePoint 开发人员,请参阅Chris O' Brien 的这篇博客