以字典作为数据源的 C# DropDownList
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/805595/
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
C# DropDownList with a Dictionary as DataSource
提问by VansFannel
I want to set DataTextField
and DataValueField
of a Dropdownlist
(languageList) using a Dictionary (list) of languageCod
(en-gb) as key and language name (english) as the text to display.
我想设置DataTextField
和DataValueField
一个中Dropdownlist
使用的词典(名单)(languageList)languageCod
(EN-GB)的关键和语言名称(英文)作为文本显示。
Relevant Code:
相关代码:
string[] languageCodsList= service.LanguagesAvailable();
Dictionary<string, string> list =
new Dictionary<string, string>(languageCodsList.Length);
foreach (string cod in languageCodsList)
{
CultureInfo cul = new CultureInfo(cod);
list.Add(cod, cul.DisplayName);
}
languageList.DataSource = list;
languageList.DataBind();
How can I set DataTextField
and DataValueField
?
我该如何设置DataTextField
和DataValueField
?
采纳答案by Canavar
Like that you can set DataTextField and DataValueField of DropDownList using "Key" and "Value" texts :
像那样,您可以使用“键”和“值”文本设置 DropDownList 的 DataTextField 和 DataValueField :
Dictionary<string, string> list = new Dictionary<string, string>();
list.Add("item 1", "Item 1");
list.Add("item 2", "Item 2");
list.Add("item 3", "Item 3");
list.Add("item 4", "Item 4");
ddl.DataSource = list;
ddl.DataTextField = "Value";
ddl.DataValueField = "Key";
ddl.DataBind();
回答by Jon Skeet
When a dictionary is enumerated, it will yield KeyValuePair<TKey,TValue>
objects... so you just need to specify "Value" and "Key" for DataTextField
and DataValueField
respectively, to select the Value/Keyproperties.
当字典被枚举时,它会产生KeyValuePair<TKey,TValue>
对象......所以你只需要分别为DataTextField
和指定“值”和“键” DataValueField
,以选择值/键属性。
Thanks to Joe's comment, I reread the question to get these the right way round. Normally I'd expect the "key" in the dictionary to be the text that's displayed, and the "value" to be the value fetched. Your sample code uses them the other way round though. Unless you really need them to be this way, you might want to consider writing your code as:
感谢 Joe 的评论,我重新阅读了这个问题以正确解决这些问题。通常我希望字典中的“键”是显示的文本,而“值”是获取的值。但是,您的示例代码以相反的方式使用它们。除非您真的需要它们这样,否则您可能需要考虑将代码编写为:
list.Add(cul.DisplayName, cod);
(And then changing the binding to use "Key" for DataTextField
and "Value" for DataValueField
, of course.)
(然后更改绑定以使用 "Key" forDataTextField
和 "Value" for DataValueField
,当然。)
In fact, I'd suggest that as it seems you really do want a listrather than a dictionary, you might want to reconsider using a dictionary in the first place. You could just use a List<KeyValuePair<string, string>>
:
事实上,我建议您似乎确实想要一个列表而不是字典,因此您可能首先要重新考虑使用字典。你可以只使用一个List<KeyValuePair<string, string>>
:
string[] languageCodsList = service.LanguagesAvailable();
var list = new List<KeyValuePair<string, string>>();
foreach (string cod in languageCodsList)
{
CultureInfo cul = new CultureInfo(cod);
list.Add(new KeyValuePair<string, string>(cul.DisplayName, cod));
}
Alternatively, use a list of plain CultureInfo
values. LINQ makes this really easy:
或者,使用普通CultureInfo
值列表。LINQ 让这一切变得非常简单:
var cultures = service.LanguagesAvailable()
.Select(language => new CultureInfo(language));
languageList.DataTextField = "DisplayName";
languageList.DataValueField = "Name";
languageList.DataSource = cultures;
languageList.DataBind();
If you're not using LINQ, you can still use a normal foreach loop:
如果您不使用 LINQ,您仍然可以使用普通的 foreach 循环:
List<CultureInfo> cultures = new List<CultureInfo>();
foreach (string cod in service.LanguagesAvailable())
{
cultures.Add(new CultureInfo(cod));
}
languageList.DataTextField = "DisplayName";
languageList.DataValueField = "Name";
languageList.DataSource = cultures;
languageList.DataBind();
回答by user98296
Just use "Key" and "Value"
只需使用“键”和“值”
回答by Matt Frear
If the DropDownList is declared in your aspx page and not in the codebehind, you can do it like this.
如果 DropDownList 在您的 aspx 页面中声明而不是在代码隐藏中,您可以这样做。
.aspx:
.aspx:
<asp:DropDownList ID="ddlStatus" runat="server" DataSource="<%# Statuses %>"
DataValueField="Key" DataTextField="Value"></asp:DropDownList>
.aspx.cs:
.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
ddlStatus.DataBind();
// or use Page.DataBind() to bind everything
}
public Dictionary<int, string> Statuses
{
get
{
// do database/webservice lookup here to populate Dictionary
}
};