dynamically fill a selectbox

  • Thread starter Thread starter Stefan Richter
  • Start date Start date
S

Stefan Richter

I would like to fill a Selectbox dynamically.
I am taking all values from a db, and fill them into a selectbox.
I guess the best way was to use some kind of a dynamical array, where you
don't have to say how many
elements it will contain.
In Java there exists special containers to achieve that, but what about VB?
I thought about somethign like that:

select year from db

while dynamischesArray.AddNewValue(nextResult)



How to get this done as easy and good as possible?

Thanks,

Stefan
 
For aWebForm, if you mean by selectbox a ListBox, then here is an example:

For ds a DataSet, states.xml an XML file containing a list of states with
Field1 containing the state abbreviation, you would do the following to
populate the list box DropDownStateList:

ds.Clear()
ds.ReadXml(Server.MapPath(".") & "\states.xml")
Me.DropDownStateList.DataSource = ds
Me.DropDownStateList.DataTextField = "Field1"
Me.DropDownStateList.DataBind()


For aWindowsForm, and a ComboBox, it would be something like

ds.Clear()
ds.ReadXml("..\states.xml")
Me.ComboBoxState.DataSource = ds.Tables(0)
Me.ComboBoxState.DisplayMember = "Field2" 'name
Me.ComboBoxState.ValueMember = "Field1" 'abreviation
 
I found it by myself:

Dim yearArrayList As New ArrayList

While (MyDataReader.Read())

yearArrayList.Add(MyDataReader.Item("year"))
 
* "Stefan Richter said:
I found it by myself:

Dim yearArrayList As New ArrayList

While (MyDataReader.Read())

yearArrayList.Add(MyDataReader.Item("year"))

What's a selectbox? The questions remains unanswered ;-).
 
Back
Top