具有来自Dictionary<;字符串>;,列表<;字符串>>;

本文关键字:gt 字符串 lt 列表 Dictionary | 更新日期: 2023-09-27 18:25:28

我有两个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";

当我从组合框1中选择"A"时,是否可以显示组合框2的值("A1"、"A2"、"A3"、"A4"、"A5")?

具有来自Dictionary<;字符串>;,列表<;字符串>>;

没有太多事情要做。答案已经很长了。Bt我们不会需要那么多。这是代码:

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,然后设置数据源。