Windows Develop Bookmark and Share   
 index > Windows Forms General > Combo Box Questin VB.net 2005 Item value and item text
 

Combo Box Questin VB.net 2005 Item value and item text

I have a list of items I need in a combo box, is there a way to put both the item ID value and the TEXT that is displayed. then they slelect one I get the ID Value not the text value

Example

my data

ID TEXT

1 Texas

5 Illinois

16 New York

I show them the TEXT but ID is the number I capture

Dim objconn As New SqlConnection(Connstring)

cboPN.Items.Clear()

Dim ocomm As New SqlCommand("Select rtN_Routename from tblRoutename order by 1", objconn)

objconn.Open()

Dim myreader As SqlDataReader = ocomm.ExecuteReader

Do While myreader.Read()

cboRoute.Items.Add(myreader.Item("rtN_Routename"))

Loop

Ljhopkins  Tuesday, September 11, 2007 3:16 PM

You won't be able to bind to your data reader. Instead, use a data adapter or table adapter and fill a data table. You can bind to a data table.

I strongly encourage you to use a TableAdapter, and create a strongly typed DataSet. They are much nicer to work with than generic DataSets and DataReaders because you get autocompletion, type checking and field name verification at compile time.

Christopher Payne  Tuesday, September 11, 2007 8:23 PM

You are almost there...

Instead of adding items 'manualy', you need to set the combobox DataSource to the SqlDataReader object and set the ValueMember and DisplayMember to the Text and ID you want:

comboBox1.DataSource = reader;

comboBox1.ValueMember = "IDField";

comboBox1.DisplayMember = "TextField";

Now, you will see the text on the combobox but when calling combobox1.SelectedValue, you will get the 'int' value matching the current selection.

Eli Gazit  Tuesday, September 11, 2007 4:23 PM

so instead of doing the loop

Do While myreader.Read()

cboRoute.Items.Add(myreader.Item("rtN_Routename"))

Loop

Can I do a data bind?? to myreader?

Ljhopkins  Tuesday, September 11, 2007 6:20 PM

getting error

Complex DataBinding accepts as a data source either an IList or an IListSource.

Dim ocomm As New SqlCommand("Select rtn_ref, rtN_Routename from tblRoutename order by 1", objconn)

objconn.Open()

Dim myreader As SqlDataReader = ocomm.ExecuteReader

cboRoute.DataSource = myreader

cboRoute.ValueMember = "rtn_ref"

cboRoute.DisplayMember = "rtn_routename"

Ljhopkins  Tuesday, September 11, 2007 6:26 PM

You won't be able to bind to your data reader. Instead, use a data adapter or table adapter and fill a data table. You can bind to a data table.

I strongly encourage you to use a TableAdapter, and create a strongly typed DataSet. They are much nicer to work with than generic DataSets and DataReaders because you get autocompletion, type checking and field name verification at compile time.

Christopher Payne  Tuesday, September 11, 2007 8:23 PM

Opps. Sorry for the wrong example...

Christopher is right, a strongly types DataSet is a good way to go.

If you want just a simple databinding, you can load the SqlReader into a DataTable and use it as the DataSource.

System.Data.DataTable dt = new System.Data.DataTable();

dt.Load(reader);

comboBox1.DataSource = dt;

Eli Gazit  Wednesday, September 12, 2007 9:46 AM

You can use google to search for other answers

Custom Search

More Threads

• How does a scrollbar work (internally)?
• richtextbox text format
• calling graphics object from textbox?
• Screen flicker when setting Opacity
• Speedometer in .net?
• Compile J2EE in Visual Studio 2003 and create exe of .Net
• VS2005 RC SplitContainer Designer Bug!
• webbrowser search history - newbie
• Problem saving a word document using DSOFramer ActiveX control
• Startup Switch