Click here to Skip to main content
Click here to Skip to main content

Load a ComboBox at Runtime

, 7 Jan 2011
Rate this:
Please Sign up or sign in to vote.
Learn how to use a dataset object to load a combobox (or other similar objects) at runtime instead of needing to set anything up at design time.
Have you ever come across a problem that is WAY too simple to be this hard? I recently stumbled into a situation like that. The concept seemed simple enough. I needed to populate a ComboBox on a .NET form at runtime. The data that needed to go into it would come in the form of a DataSet. This can’t be hard, right? ComboBoxes take DataSets at design time so I figured I would just dump the DataSet in at runtime, set up the DisplayMember and ValueMember, and I would be good to go.
 
I tried to do the following:
'dst is our pre-loaded DataSet.  It has
'two fields: ID and Value.
ComboBox1.DataSource = dst
ComboBox1.DisplayMember = "Value"
ComboBox1.ValueMember = "ID"
 
Unfortunately, this throws the error “Cannot bind to the new display member. Parameter name: newDisplayMember”. So that doesn’t work. I did some research and came across an article that indicated that you needed to assign the DisplayMember and ValueMember before the DataSource was assigned so that it would populate correctly, so I tried it using the following code:
 
'dst is our pre-loaded DataSet.  It has
'two fields: ID and Value.
ComboBox1.DisplayMember = "Value"
ComboBox1.ValueMember = "ID"
ComboBox1.DataSource = dst
 
This time it actually ran (no exceptions thrown) but when I looked at the items in the ComboBox, there was only one item (there should have been 10) and its text was “System.Data.DataViewManagerListItemTypeDescriptor”.
 
Once again I turned to Google for the answer. This time, I came across an article that pointed out that you needed to specify which table in the DataSet you were attempting to access. That made sense, so I tried it like so:
'dst is our pre-loaded DataSet.  It has
'two fields: ID and Value.
ComboBox1.DataSource = dst.Tables("ComboList")
ComboBox1.DisplayMember = "Value"
ComboBox1.ValueMember = "ID"
 
Finally, this worked as intended. The ComboBox populated and the values it returned were correct. What seemed to be a simple problem ended up taking half an hour to resolve.
 
As a side note, I did run across one more issue that might catch some of you off guard. When it seems like everything is set up right but for some reason the entire list is populated with “System.Data.DataRowView” entries (or the value of a particular selection comes out as this), you have mis-labeled your column. This is the system’s way of telling you that you made a mistake. Just check the column names and try again.
 
So, I have a working solution. I looked into extracting this functionality into its own function in order to make my life easier. After I got all done, I wasn’t sure that this added any improvements or if it just changed how I entered the information. I’ll give you the code and let you decide:
 
'A much simpler call that assumes the table is the first table
'in the DataSet is the correct one and it assumes the first
'column is the identity column and the second one is the data
'column.
Public Sub PopulateCombo(ByRef cbo As Windows.Forms.ComboBox, _
                         ByVal dst As DataSet)
    'Calls the full method using our assumptions
    PopulateCombo(cbo, dst, dst.Tables(0).Columns(0).ToString, _
                  dst.Tables(0).Columns(1).ToString, _
                  dst.Tables(0).ToString)
End Sub
'The complete method that populates a ComboBox reference with
'data from the created DataSet.  This is the full method.
Public Sub PopulateCombo(ByRef cbo As Windows.Forms.ComboBox, _
                         ByVal dst As DataSet, _
                         ByVal strValueMember As String, _
                         ByVal strDisplayMember As String, _
                         ByVal strTableName As String)
    With cbo
        .DataSource = dst.Tables(strTableName)
        .DisplayMember = strDisplayMember
        .ValueMember = strValueMember
    End With
End Sub
 
To call this method, you would use the following code in the OnLoad section of your Form:
 
'Load the ComboBox from our DataSet
PopulateCombo(ComboBox1, dst)
 
So, that turns a few lines into one and it is clearer to read. If you are going to be doing this multiple times, it might be useful. It might also help if you wanted to create a class to manage this method, then add a section for storing DataSets. This way, you could load the data from the database once and then call it over and over with the same code. Now it is starting to make sense to do it this way.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Tim Corey
Software Developer (Senior) Epicross
United States United States
I am currently a Lead Technical Consultant for a consulting company called Epicross. My primary skills are in .NET, SQL, JavaScript, and other web technologies although I have worked with PowerShell, C, and Java as well.
 
In my previous positions, I have worked as a lead developer and IT Director. As such, I have been able to develop software on a number of different types of systems and I have learned how to correctly oversee the overall direction of technology for an organization. I've developed applications for everything from machine automation to complete ERP systems.
 
My current position is mainly focused making our clients more efficient and effective. I use custom software (desktop, mobile, and web) to help facilitate this goal. When I'm not working for the company, I'm usually developing applications to fill the needs of the organizations I volunteer for.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralReason for my vote of 5 Excellent Tip. Worth the 5 I am givi... PinmemberDalek Dave10-Jan-11 11:26 
GeneralThis tip is too big to read. But it is easy to understand. PinmemberAslam_Iqbal10-Jan-11 8:07 
GeneralFor Inspiration. 5/5. Go Ahead. PinmemberAbdul Quader Mamun5-Jan-11 19:55 
GeneralReason for my vote of 5 For Inspiration-5/5! PinmemberAbdul Quader Mamun5-Jan-11 19:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 7 Jan 2011
Article Copyright 2011 by Tim Corey
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid