Binding an enum to a WinForms combo box, and then setting it

前端 未结 28 2117
心在旅途
心在旅途 2020-11-28 04:33

a lot of people have answered the question of how to bind an enum to a combo box in WinForms. Its like this:

comboBox1.DataSource = Enum.GetValues(typeof(MyE         


        
28条回答
  •  隐瞒了意图╮
    2020-11-28 05:06

    Generic method for setting a enum as datasource for drop downs

    Display would be name. Selected value would be Enum itself

    public IList> GetDataSourceFromEnum() where T : struct,IConvertible
        {
            IList> list = new BindingList>();
            foreach (string value in Enum.GetNames(typeof(T)))
            {
                list.Add(new KeyValuePair(value, (T)Enum.Parse(typeof(T), value)));
            }
            return list;
        }
    

提交回复
热议问题