最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

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

C# crifan 8871浏览

【问题】

C#中已经有了一个ComboBox和一个特定的结构体数组:

private struct keyValueList
{
    public string Key{get;set;} // key
    public List<string> ValueStrList{get;set;} // the string value list for the key
}
 
List<keyValueList> gFootprintTypeSelList; // footprint type
 
private void initFootprintTypeAndFootprintString()
{
    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);
 
    //2. option2: Blog Commenting
    keyValueList keyValueListBlogCommenting = new keyValueList();
    keyValueListBlogCommenting.Key = "Blog Commenting";
    keyValueListBlogCommenting.ValueStrList = new List<string>();
    keyValueListBlogCommenting.ValueStrList.Add("KeywordLuv Blogs");
    keyValueListBlogCommenting.ValueStrList.Add("Joomla JComments Plugin");
    keyValueListBlogCommenting.ValueStrList.Add("Yootheme Zoo Blog App");
    keyValueListBlogCommenting.ValueStrList.Add("CommentLuv Premium Blogs");
    keyValueListBlogCommenting.ValueStrList.Add("Anchor Text In Comment Blogs");
    keyValueListBlogCommenting.ValueStrList.Add("Do Follow Comment Blogs");
    keyValueListBlogCommenting.ValueStrList.Add("Create your own .GOV WordPress site");
    keyValueListBlogCommenting.ValueStrList.Add("Create your own .EDU WordPress site");
    keyValueListBlogCommenting.ValueStrList.Add("Find .edu Blogs");
    keyValueListBlogCommenting.ValueStrList.Add("Find .gov Blogs");
    gFootprintTypeSelList.Add(keyValueListBlogCommenting);
 
    //3. option3: Message Boards and Forums
    keyValueList keyValueListMessageBoardsAndForums = new keyValueList();
    keyValueListMessageBoardsAndForums.Key = "Message Boards and Forums";
    keyValueListMessageBoardsAndForums.ValueStrList = new List<string>();
    keyValueListMessageBoardsAndForums.ValueStrList.Add("PHPbb");
    keyValueListMessageBoardsAndForums.ValueStrList.Add("vBulletin");
    keyValueListMessageBoardsAndForums.ValueStrList.Add("SMF");
    keyValueListMessageBoardsAndForums.ValueStrList.Add("Simple Machines");
    keyValueListMessageBoardsAndForums.ValueStrList.Add("punBB");
    keyValueListMessageBoardsAndForums.ValueStrList.Add("Expression Engine");
    keyValueListMessageBoardsAndForums.ValueStrList.Add("Blog Engine");
    gFootprintTypeSelList.Add(keyValueListMessageBoardsAndForums);
 
    //4. option4: Advanced Search Parameters
    keyValueList keyValueListAdvancedSearchParameters = new keyValueList();
    keyValueListAdvancedSearchParameters.Key = "Advanced Search Parameters";
    keyValueListAdvancedSearchParameters.ValueStrList = new List<string>();
    keyValueListAdvancedSearchParameters.ValueStrList.Add("URLs Containing Keyword");
    keyValueListAdvancedSearchParameters.ValueStrList.Add("Page Titles Containing Keyword");
    keyValueListAdvancedSearchParameters.ValueStrList.Add("Search Anchor Text");
    keyValueListAdvancedSearchParameters.ValueStrList.Add("Related (enter a domain)");
    gFootprintTypeSelList.Add(keyValueListAdvancedSearchParameters);
 
    //5. option5: Sponsor/Donate
    keyValueList keyValueListSponsorDonate = new keyValueList();
    keyValueListSponsorDonate.Key = "Sponsor/Donate";
    keyValueListSponsorDonate.ValueStrList = new List<string>();
    keyValueListSponsorDonate.ValueStrList.Add("Sponsor");
    keyValueListSponsorDonate.ValueStrList.Add("Sponsorship");
    keyValueListSponsorDonate.ValueStrList.Add("Benefactors");
    keyValueListSponsorDonate.ValueStrList.Add("Sponsor Charity");
    keyValueListSponsorDonate.ValueStrList.Add("Donate");
    keyValueListSponsorDonate.ValueStrList.Add("Donations");
    keyValueListSponsorDonate.ValueStrList.Add("Donors");
    gFootprintTypeSelList.Add(keyValueListSponsorDonate);
 
    //6. option6: Wiki / Media Wiki Pages
    keyValueList keyValueListWikiMediaWikiPages = new keyValueList();
    keyValueListWikiMediaWikiPages.Key = "Wiki / Media Wiki Pages";
    keyValueListWikiMediaWikiPages.ValueStrList = new List<string>();
    keyValueListWikiMediaWikiPages.ValueStrList.Add("Wiki sites");
    keyValueListWikiMediaWikiPages.ValueStrList.Add("Outdated Wiki Content");
    gFootprintTypeSelList.Add(keyValueListWikiMediaWikiPages);
 
    //7. option7: Other Queries
    keyValueList keyValueListOtherQueries = new keyValueList();
    keyValueListOtherQueries.Key = "Other Queries";
    keyValueListOtherQueries.ValueStrList = new List<string>();
    keyValueListOtherQueries.ValueStrList.Add("Directory Add URL");
    keyValueListOtherQueries.ValueStrList.Add("Directory Site");
    keyValueListOtherQueries.ValueStrList.Add("Hubpages - Hot Hubs");
    keyValueListOtherQueries.ValueStrList.Add("Squidoo lenses - Add To List");
    keyValueListOtherQueries.ValueStrList.Add("Social Bookmarking");
    keyValueListOtherQueries.ValueStrList.Add("Social Bookmarking 2.0");
    keyValueListOtherQueries.ValueStrList.Add("Guestbooks");
    keyValueListOtherQueries.ValueStrList.Add("Suggest or Submit URL");
    keyValueListOtherQueries.ValueStrList.Add("Warp Framework (Joomla)");
    keyValueListOtherQueries.ValueStrList.Add("Angelae8654 Site Profiles");
    gFootprintTypeSelList.Add(keyValueListOtherQueries);
 
    //8. option8: Link Bartering
    keyValueList keyValueListLinkBartering = new keyValueList();
    keyValueListLinkBartering.Key = "Link Bartering";
    keyValueListLinkBartering.ValueStrList = new List<string>();
    keyValueListLinkBartering.ValueStrList.Add("Buy Blog Posts");
    keyValueListLinkBartering.ValueStrList.Add("Buy Link");
    keyValueListLinkBartering.ValueStrList.Add("Link Exchange");
    keyValueListLinkBartering.ValueStrList.Add("Link Barter Service");
    gFootprintTypeSelList.Add(keyValueListLinkBartering);
 
    //9. option9: Site Specific Sites
    keyValueList keyValueListSiteSpecificSites = new keyValueList();
    keyValueListSiteSpecificSites.Key = "Site Specific Sites";
    keyValueListSiteSpecificSites.ValueStrList = new List<string>();
    keyValueListSiteSpecificSites.ValueStrList.Add("Facebook");
    keyValueListSiteSpecificSites.ValueStrList.Add("Twitter");
    keyValueListSiteSpecificSites.ValueStrList.Add("Pinterest");
    keyValueListSiteSpecificSites.ValueStrList.Add("Google Groups");
    keyValueListSiteSpecificSites.ValueStrList.Add("Scribd");
    keyValueListSiteSpecificSites.ValueStrList.Add("Slideshare");
    keyValueListSiteSpecificSites.ValueStrList.Add("Wikipedia");
    keyValueListSiteSpecificSites.ValueStrList.Add("Tumblr");
    keyValueListSiteSpecificSites.ValueStrList.Add("Typepad");
    keyValueListSiteSpecificSites.ValueStrList.Add("Buzzfeed");
    keyValueListSiteSpecificSites.ValueStrList.Add("NYTimes");
    keyValueListSiteSpecificSites.ValueStrList.Add("Huffington Post");
    gFootprintTypeSelList.Add(keyValueListSiteSpecificSites);
}

现在希望把此结构体数组gFootprintTypeSelList,当做ComboBox变量cmbFootprintType的数据源。

希望实现下拉菜单中,可以显示对应的名字。以及获得对应选中项的值,是其中的List<string>的ValueStrList。

【解决过程】

1.参考了之前的自己的:

【已解决】C#中向Combox中添加结构体数组

【已解决】C#中ComboBox动态绑定赋值

去写代码:

cmbFootprintType.DataSource = gFootprintTypeSelList;
cmbFootprintType.DisplayMember = "key";

就可以正常显示所要的结果了:

foot print combobox can show select list

【总结】

动态的给ComboBox绑定数据源:

1.设置结构体,其中的field的名字首字母大写,支持get和set;

2.创建对应的结构体数组

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

即可。

转载请注明:在路上 » 【已解决】C#中给ComboBox设置数据源

81 queries in 0.183 seconds, using 22.10MB memory