1.3.1. 给ComboBox设置数据源

主要包含三步:

  1. 设置结构体,其中的field的名字首字母大写,支持get和set
    
    private struct keyValueList
    {
        public string Key{get;set;} // key
        public List<string> ValueStrList{get;set;} // the string value list for the key
    }
    
                    

  2. 创建对应的结构体数组
    
    List<keyValueList> gFootprintTypeSelList; // footprint type
    
    gFootprintTypeSelList = new List<keyValueList>();
    
    //1. option1: Guest Blogging
    keyValueList keyValueListGuestBlogging = new keyValueList();
    keyValueListGuestBlogging.Key = "Guest Blogging";
    keyValueListGuestBlogging.ValueStrList = new List<string>();
    //keyValueListGuestBlogging.ValueStrList.Add("Specify Footprint");
    keyValueListGuestBlogging.ValueStrList.Add("Guest Blogging");
    keyValueListGuestBlogging.ValueStrList.Add("Contribute");
    keyValueListGuestBlogging.ValueStrList.Add("Write for us");
    keyValueListGuestBlogging.ValueStrList.Add("Guest Category");
    keyValueListGuestBlogging.ValueStrList.Add("Submit Content");
    gFootprintTypeSelList.Add(keyValueListGuestBlogging);
     
    
                    

  3. 把结构体数组赋值给ComboBox的DataSource,把DisplayMember设置为对应的结构体的field的名字(全小写)
    
    cmbFootprintType.DataSource = gFootprintTypeSelList;
    cmbFootprintType.DisplayMember = "key";
    
                    

详见:【已解决】C#中给ComboBox设置数据源