Combo Box binding Question

QuestionTime

New member
Joined
Jul 29, 2006
Messages
4
Programming Experience
Beginner
Hi guys:

I was wondering when using datareader to loop through DB records like the following code:

While DBDataReader.Read
cboCombox.Items.Add(DBDataReader(sColName))
End While

It will inserted as display Member in the combo box, however, it will never insert value member to it. Let's say my SQL statement = "Select Name, ID from Customers". What I would want to insert the Name as display member and ID as value member.
I knew it can be done by assign the dataset to datasource, however, I will lose ability to enter the custome text like "==== Select All ====".
If you have an idea how to assign value member through DataReader, please let me know.

Thank you so much.


 
Problem you've got there is that a value member is used as the lookup field in a bound combo. If you are using a datareader you are just adding objects to the combo's collection. If you want to use the value member then you're gonna need to bind it to something. I suppose what you could do here is populate yuor combo with the datareader when the user selects an item run a loop to go through the datasource (whatever it may be) and compare the values, then extract the ID when you find a match. Not very efficient mind you.
 
Last edited:
oh, ick....

Better yet, skip the reader alltogether, .Fill a dataTable, set the dataTable as the DataSource, set the DisplayMember as the field name that you want displayed, and then set the ValueMember to the name of the field you want as the value.

-tg
 
Yea i know, awful, but the reason i said it was because he wants to be able to keep his 'Select All' item at the top of the combobox. Which when databound isn't possible without creating a custom control
 
Back
Top