1. 程式人生 > >Winform Comobox 繫結 Dictionary資料來源

Winform Comobox 繫結 Dictionary資料來源

使用BindingSource進行間接繫結

    Dictionary<string ,string> dic=new Dictionary<string,string>();
            BindingSource bs = new BindingSource();     //宣告BindingSource
            
            dic.Add("GROUP_NAME","工序名稱");
            dic.Add("TEST_ITEM_NAME", "測試專案名稱");
            dic.Add("PERSON_MODIFY", "修改人");

            bs.DataSource = dic;        //繫結Dictionary



            this.cbb_ChooseItem.DataSource = bs;    //繫結BindingSource
            this.cbb_ChooseItem.ValueMember = "Key";
            this.cbb_ChooseItem.DisplayMember = "Value";

使用:

string chooseItem = this.cbb_ChooseItem.SelectedValue.ToString();