ComboBox控件 Dictionary加载数据源

  #region [设定ComboBox数据源]
        private Dictionary<string, string> GetScoreStatus()
        {
            Dictionary<string, string> dic = new Dictionary<string, string>
            {
                {"2", "全部"},
                {"1", "成功"},
                {"0", "失败"}
            };
            return dic;
        }
        #endregion

        #region [绑定ComboBox成绩上传状态]
        private void BindComboBox()
        {
            ComboBox comboBox = this.toolStripComboBox_ScoreStatus.ComboBox;
            if (comboBox != null)
            {
                BindingSource bs = new BindingSource        //声明BindingSource
                {
                    DataSource = GetScoreStatus()           //绑定Dictionary
                };
                comboBox.DataSource = bs;                    //绑定BindingSource
                comboBox.ValueMember = "Key";
                comboBox.DisplayMember = "Value";
            }
        }
        #endregion
 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedValue is string)
            {
                string selectText = comboBox1.SelectedValue.ToString();
            }
           
            
        }

本文来自博客园,作者:云辰,转载请注明原文链接:https://www.cnblogs.com/yunchen/p/13814769.html

原文地址:https://www.cnblogs.com/yunchen/p/13814769.html