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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combo box Initial Values

Status
Not open for further replies.

Lenvdb

Programmer
Jun 21, 2002
81
GB
Heya Gurus!
Upon loading a form I load all the combo boxes with lists of data from tables etc.
I initially want all the combo boxes to show no selected values. I set all the selectedindex properties to -1 but they all seem to default to 0, which is usually the first item in the list.

How do I get this right?

It worked fine in VB6 because by default the Listindex property was set to -1.

Rgds
B-0
Len



 
A databound combo will, by default, show the first item in the datasource. The following cheat works (or at least it did for me...):
Code:
ComboBox1.SelectedIndex = ComboBox1.FindStringExact("")
 
Ignore previous post - clearly had too many beers last night...

Sorry!
 
No Problem mate..so it is a problem then with the data being bound to the ccombobox.

How do I use a SQL datareader to populate a combobox? Anyone got some examples for me?

Len
 

So ,
Just looking at your posts it looks like a mission to use a datareader for populating combo boxes and lists...

Its much easier to just use datasets and data adapters and bind them to the controls. Am I right in my assumption?

Unless you convince me otherwise...

B-)
Len

 
(Brain now in gear...)

What I usually do is populate a datatable and then, before I bind it to a combo, insert a row with the text 'Select an option'. Such as:
Code:
Dim dr As DataRow = DataTable.NewRow

dr(0) = "0"
dr(1) = "Select an option"

DataTable.Rows.InsertAt(dr, 0)

You can then use something like
Code:
If ComboBox.SelectedValue = "0" Then
to make sure the user has selected a valid item.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top