C# DropDownList 以字典作为数据源 [英] C# DropDownList with a Dictionary as DataSource
本文介绍了C# DropDownList 以字典作为数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 languageCod的字典(列表)设置
Dropdownlist
(languageList)的 DataTextField
和 DataValueField
code> (en-gb) 作为键和语言名称(英语)作为要显示的文本.
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.
相关代码:
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();
如何设置DataTextField
和DataValueField
?
推荐答案
就像你可以使用Key"和Value"文本设置DropDownList的DataTextField和DataValueField:
Like that you can set DataTextField and DataValueField of DropDownList using "Key" and "Value" texts :
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();
这篇关于C# DropDownList 以字典作为数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文