ComboBox 動態綁定數據

 Dictionary<string, int> dic = new Dictionary<string, int>();

            dic.Add("現金", 0);
            dic.Add("銀行卡", 1);
            dic.Add("信用卡", 2);
            dic.Add("股票", 3);
            dic.Add("基金", 4);
            dic.Add("其他", 5);

            BindingSource bs = new BindingSource();
            bs.DataSource = dic;

            comboBox1.ValueMember = "value";
            comboBox1.DisplayMember = "key";
            comboBox1.DataSource = bs;

            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;

獲取選中的值:
 MessageBox.Show(comboBox1.SelectedValue.ToString());
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章