populate a listbox with a hashtable

D

Dave

Hi all,
Is it possible to populate a listbox from a hashtable (complexbind) where
the ValueMember of the listbox maps to the "key" member of the hashtable AND
the DisplayMember of the listbox maps to the "value" member of the
hashtable?
I found that it is impossible to use a hashtable as a listbox datasource
directly (from trial and error) and tried DirectCast-ing to an arraylist
without any success. Before I spend anymore time in vain complex binding, I
thought I'd ask to see if anyone has figured a method of doing so.

D
 
Z

Zoury

oh sorry! i didn't pay much attention to the properties name from the
sample.

have you try this :
'***
Dim ht As Hashtable = New Hashtable(3)
ht.Add("item 1", 1)
ht.Add("item 2", 2)
ht.Add("item 3", 3)

ListBox1.DisplayMember = "key"
ListBox1.ValueMember = "value"

Dim entry As DictionaryEntry
For Each entry In ht
ListBox1.Items.Add(entry)
Next
'***

note that for some reason you must affect DisplayMember and ValueMember
properties before adding the items or the displayed item might not be
correct... (plus i'm not quite sure why it's not the other way around)
 
D

Dave

Thanks Zoury!

I learned something new: looping with the "DictionaryEntry" - This is
awesome and works faster than the datatable binding!
Forgive me for getting so excited over this, I am quite new to VB.Net!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top