Solved

combo box and sqlclient

Posted on 2004-08-18
8
329 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
Comment Utility
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
Comment Utility
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
Comment Utility
Cant we set the datasource of combo box to a SqlDataReader
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 25

Expert Comment

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

Expert Comment

by:RonaldBiemans
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now