2个包含来自Dictionary <string>的DatasSource的组合框,List <string>&gt; </string> </string>

时间:2014-10-04 06:04:48

标签: c# dictionary combobox

我有2个commboBoxes,代码如下:

  Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>()
        {
            { "A", new List<string> { "A1", "A2", "A3", "A4", "A5" }},
            { "B", new List<string> { "B1", "B2", "B3", "B4", "B5" }},
            { "C", new List<string> { "C1", "C2", "C3", "C4", "C5" }},
        };

        comboBox1.DataSource = dict.ToList();
        comboBox1.DisplayMember = "Key";

是否可以显示值(&#34; A1&#34;,&#34; A2&#34;,&#34; A3&#34;,&#34; A4&#34;,&#34 ;当我选择&#34; A&#34;时,A5&#34;)到comboBox2来自comboBox1?

1 个答案:

答案 0 :(得分:0)

没有太多工作要做。它已经得到了很多回答。但是我们不需要这么多。这是代码:

Dictionary<string, List<string>> dict;       
    private void Form1_Load(object sender, EventArgs e)
    {
        dict = new Dictionary<string, List<string>>()
        {
            { "A", new List<string> { "A1", "A2", "A3", "A4", "A5" }},
            { "B", new List<string> { "B1", "B2", "B3", "B4", "B5" }},
            { "C", new List<string> { "C1", "C2", "C3", "C4", "C5" }},
        };
        cb1.DisplayMember = "Key";
        cb1.DataSource = dict.ToList();

    }

    private void cb1_SelectedIndexChanged(object sender, EventArgs e)
    {                      
            cb2.DataSource = dict[cb1.Text];              
    }

修改是正确的: 首先设置DisplayMember,然后设置数据源。