Dictionary绑定到ComboBox显示值,实际值

首次发布:2018-03-29 13:19
相关视频教程 https://www.bilibili.com/video/BV19M4y1N7bt
        private void TestAndSetForm_Load(object sender, EventArgs e)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            BindingSource bs = new BindingSource();
            dic["广东省"] = "http://mk.gd.soa.csg.cn";
            dic["云南省"] = "http://mk.yn.soa.csg.cn";
            dic["贵州省"] = "http://mk.gz.soa.csg.cn";
            dic["海南"] = "http://mk.hn.soa.csg.cn";
            dic["广西省"] = "http://mk.gx.soa.csg.cn";
            
            bs.DataSource = dic;
            comboBox1.DataSource = bs;
            comboBox1.DisplayMember = "Key";
            comboBox1.ValueMember = "Value";
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            string s = comboBox1.Text;//显示值 广东省
            string s2 = comboBox1.SelectedValue.ToString();//实际值 http://mk.gd.soa.csg.cn
        }