There is no ’ListItem’ in Winforms Combobox
I have been using ASP.Net for years. There is a very popup control, dropdown list. I used this control thousands of times. I know it very well. Recently, I built a winforms application. I need to use a dropdown list. ComboBox Control is the most similar with dropdown list. I found there is a different. I like to add a default option, new ListItem("Please select a type",""), into the items of the control. If the program will check whether the user selects this option, if that is the case, it will treat the input to be null. The items in ComoboBox is ObjectCollection. They are just a list of objects. You cannot use ListItem.
Well, you can create your version of ListItem objects and convert your domain objects into ListItem. Finally, you can bind the list of ListItem objects into the comobobox. In this way, Combobox will works like a drop down list.
I chose to use a simpler approach. I just created a domain object with ID 0 and insert it into the list.
Code
ResourceType empty = new ResourceType(); | |
empty.Name="---------------------------------------"; | |
resourceTypes.Insert(0,empty); | |
ddlSearchResourceTypes.DataSource=resourceTypes; |
Trackback address for this post
Trackback URL (right click and copy shortcut/link location)
Feedback awaiting moderation
This post has 46 feedbacks awaiting moderation...
Form is loading...