Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to populate a combobox and listbox

Status
Not open for further replies.

enak

Programmer
Jul 2, 2002
412
US
What is the best way to populate a combobox and a listbox? I need to populate them from a database. I can do it with the following code:

While dr.Read
Me.lstClassifications.Items.Add(dr.GetValue(1))
End While

but I am not sure that I am getting id's along with the display value.

I am using a datareader but when I use the code that I find here I get an unhandled exception.

This is for a window application using VB.NET.

Please help

Thanks,
enak.
 
This does not work for me. I get this error:

An unhandled exception of type 'System.Exception' occurred in system.windows.forms.dll

Additional information: Complex DataBinding accepts as a data source either an IList or an IListSource

Any Ideas how to fix it?
 
I've not tested anything but you will probably need to use a dastaset rather than a datareader to take advantage of Rick's suggestion.

Hope this helps.

[vampire][bat]
 
If you want to get id's along with the display value, you can do data binding. However, you can also do it "by hand."

The .Net ListBox is much more flexible than its predecessors. While the ItemData is no longer supported, it's superfluous, because the ListBox actually holds an array of objects. So, you can create a class that holds any information that you want, and populate the ListBox's Items collection with instances of it.

Ok. Create a class that identifies a list item, such as ListItem. You'll have to override its default ToString method, since the default implementation of ToString comes from System.Object, and returns the string value of the class from which the object was instantiated (in this case, all of your listbox lines would say something like "System.Object.myListItem", as I learned the hard way).

Code:
Class myListItem
   Public myID as Integer
   Public myString as String

   Sub New(ByVal i As Integer, ByVal s As String)
      Me.myId = i
      Me.myString = s
   End Sub

   Overrides Function ToString() As String
      Return myString
   End Function
End Class
Now, to use it in a ListBox:
Code:
With ListBox1.Items
   .Add(New myListItem(1, "Peter Piper"))
   .Add(New myListItem(2, "Pickled Peppers"))
   'etc.
End With
To access the myId property above, you'll have to recast the list item to what it was (the Add method converts the object into an Item object--I think--so you have to cast it back to access the properties):
Code:
myInteger = DirectCast(ListBox1.Items(1), myListItem).myId
Et voila! This has always worked for me.

HTH

Bob
 
By the way, I use a DataReader to get my data with this technique.
 
That way you can, Bob, because you are doing everything manually. I was simply commenting on Rick's suggestion.


Hope this helps.

[vampire][bat]
 
Perhaps,
Code:
While dr.Read
    Me.lstClassifications.Items.Add(dr(1).ToString)
End While
Make sure that you have the index 1 in dr.

Regards,
mansii
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top