【问题】
C#中已经有了一个ComboBox和一个特定的结构体数组:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | 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.参考了之前的自己的:
和
去写代码:
1 2 | cmbFootprintType.DataSource = gFootprintTypeSelList; cmbFootprintType.DisplayMember = "key" ; |
就可以正常显示所要的结果了:
【总结】
动态的给ComboBox绑定数据源:
1.设置结构体,其中的field的名字首字母大写,支持get和set;
2.创建对应的结构体数组
3.把结构体数组赋值给ComboBox的DataSource,把DisplayMember设置为对应的结构体的field的名字(全小写)
即可。
转载请注明:在路上 » 【已解决】C#中给ComboBox设置数据源