Solved

combo box and sqlclient

Posted on 2004-08-18
8
335 Views
Last Modified: 2011-09-20
Hi,
I use SQL DataReader to get data from sql then add to combo box:

cboTest.Items.Add("xxx")
With this way, I just can set the display member of the box, but cannot set the value inside. Anyone can help?

Another way: Set DataSource property of combo box to a table of dataset.

Could you tell me the faster way to write this ? Or another way?
Thanks, PN.
0
Comment
Question by:phuongnguyen
  • 5
8 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11829489
try something like this

Dim MyList as new sortedList

Do while myReader.Read
     dim strItem as string
     dim strItemID as string
     strItem = myReader("yourItem").ToString
     strItemID = myReader("yourItemID").ToString
     idList.Add(strItemID, strItem)
Loop

Now, bind the sortedlist to the combobox

cbotest.DataSource = idList
cbotest.DisplayMember = idlist.value
cbotest.ValueMemeber = idlist.key
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11829891
sorry, this should be correct

Dim MyList as new sortedList

Do while myReader.Read
     dim strItem as string
     dim strItemID as string
     strItem = myReader("yourItem").ToString
     strItemID = myReader("yourItemID").ToString
     Mylist.Add(strItemID, strItem)
Loop

Now, bind the sortedlist to the combobox
cbotest.DataSource = Mylist
cbotest.DisplayMember = Mylist.value
cbotest.ValueMemeber = Mylistlist.key
0
 
LVL 7

Expert Comment

by:ramesh12
ID: 11831805
Cant we set the datasource of combo box to a SqlDataReader
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11838668
Not directly, only the way you tried or like my way
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11838675
Otherwise use a datatable or dataset they can directly be used as a datasource
0
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 100 total points
ID: 11842824
Forget my previous suggestion this is the way to go

        Dim myList As New ArrayList

        While myReader.Read()
            myPeople.Add(New DictionaryEntry(myReader("yourid"), myReader("youritem")))
        End While

        Me.ComboBox1.DataSource = myList
        Me.ComboBox1.DisplayMember = "Value"
        Me.ComboBox1.ValueMember = "Key"

to get the information from the combobox use

        Dim myThing As DictionaryEntry = Me.ComboBox1.SelectedItem
        MessageBox.Show(mything.Key & " - " & mything.Value)

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question