Populating a ComboBox using C#

后端 未结 10 1391
不知归路
不知归路 2020-12-02 15:55

I would like to populate a combobox with the following:

Visible item / Item Value

English / En

Italian / It

Spainish / Sp 

etc...         


        
10条回答
  •  离开以前
    2020-12-02 16:20

    To make it read-only, the DropDownStyle property to DropDownStyle.DropDownList.

    To populate the ComboBox, you will need to have a object like Language or so containing both for instance:

    public class Language {
        public string Name { get; set; }
        public string Code { get; set; }
    }
    

    Then, you may bind a IList to your ComboBox.DataSource property like so:

    IList languages = new List();
    languages.Add(new Language("English", "en"));
    languages.Add(new Language("French", "fr"));
    
    ComboxBox.DataSource = languages;
    ComboBox.DisplayMember = "Name";
    ComboBox.ValueMember = "Code";
    

    This will do exactly what you expect.

提交回复
热议问题