Dropdown question

  • Thread starter Thread starter John Wright
  • Start date Start date
J

John Wright

I have a dropdown that I load a list of values from a database. If I have
more that one value returned from a search I want to populate the dropdown
and have the first value be "<Select Run>". Then have the person use the
dropdown to select the item. If only one item is found, then I show the
item in the dropdown and load its values without a problem. Here is the
code I use to populate the dropdown. How would I add the <Select Run> item
after binding to the datatable?:

If dtCrushing.Rows.Count > 1 Then

With cboCrushingRunNumber

.DataSource = dtCrushing

.DisplayMember = "Run_Nbr"

.ValueMember = "RunID"

.SelectedIndex = 0

End With

Else

'load the data

LoadCrushingData(dtCrushing)

End If
 
There may be a better way, but what I would do, is to not 'bind' the combobox
to your datasource, but to load the the combobox 'items' collection from your
rows. Then you just have to remember that combobox.selectedindex will be 1
greater then the row it represents. So, something like this ...

If dtCrushing.Rows.Count > 1 Then
cboCrushingRunNumber.Items.Add("<Select Run>")
For each dr as DataRow in dtCrushing.Rows
cboCrushingRunNumber.Items.Add(dr.Item("Run_Nbr").ToString)
Next
.SelectedIndex = 0
Else

'load the data

LoadCrushingData(dtCrushing)

End If
 
Back
Top