C#的DropDownList用字典作为数据源 [英] C# DropDownList with a Dictionary as DataSource

查看:262
本文介绍了C#的DropDownList用字典作为数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置 DataTextField DataValueField DROPDOWNLIST (languageList)使用 languageCod (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

推荐答案

就像你可以设置使用的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屋!

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