[RESOLVED] How to get ALL the Values from a Combo Box.
I am familiar with getting a selected value from a combo box but what about if you wanted to get all the values from a combo box then store those values in an array or list.
The combo box would be data bound to a source and have an ID as the value member. So How would I get all the values from the combo box.
Thanks.
Re: How to get ALL the Values from a Combo Box.
The .Items property gives you access to all of the items, but exactly what type you see there may be not what you are expecting. I would expect that, from your description, the .Items would be a datarowview, though .Items(x) may return an object. If that is the case, you would have to get the item from the datarowview, because you are getting a whole row rather than just the display item.
Re: How to get ALL the Values from a Combo Box.
here's how:
vb Code:
Dim ids = (From item As DataRowView In ComboBox1.Items.Cast(Of DataRowView)() Select item.Item("ID")).ToArray
Re: How to get ALL the Values from a Combo Box.
Ok thanks what is "ids" is that the array?
Which array does the code fill?
Can you explain the code for me please.
Re: How to get ALL the Values from a Combo Box.
ids, in that case, will be an array because that is what the code returns. By not specifying a type, the compiler will determine which type is being returned and the variable will be that type. If you paste that code into your project you can highlight ids and it will tell you which type it is.
What that code does is take all the items in the combobox, which are type Object, and cast them to datarowviews. The code then takes the ID item of those datarowviews and packages them all into an array, which is returned.
Re: How to get ALL the Values from a Combo Box.
Ok so how would I manipulate the array. So I could get each value 1 at a time.
For example,
Code:
For Each Value In ids
ID = ids(Value)
' Do something with this data
ID = 0 ' Reset the value for loop
Next Value
OR
For index = 0 To ids.Length - 1
ID = ids(index)
' Do something...
Next
Is this right? Would this save the data in the array from 0 to last or would I need to count how many items in the array and do a different statement. Thanks.
Re: How to get ALL the Values from a Combo Box.
yes. it's just a standard array + you'd use it like any array