Solved

vb.net combobox add with index

Posted on 2006-07-11
6
816 Views
Last Modified: 2008-01-09
I am trying to convert from vb6 to .net
I can't figure out how to add an index to a combobox.
What I mean is the value associtated with the text, in this case the id from my table (row("ID").ToString).


        For Each row In table.Rows
            cmbBox.Items.Add(row("description").ToString)
        Next

        'I used to use something like this in vb6
        cmbBox.ItemData(cmbBox.newIndex) = TestsRecord.Fields("ID").value
0
Comment
Question by:jackjohnson44
  • 3
  • 2
6 Comments
 
LVL 14

Accepted Solution

by:
ptakja earned 200 total points
ID: 17082468
Since you have your data in a datatable, this is easy. The properties you are looking for is the DisplayMember & ValueMember properties:

Try databinding. It will be simpler:

cboBox.DataSource = table                          'Tells the combobox what object has the data in it.
cobBox.DisplayMember = "description"          'Field name that contains the data you want displayed in the combo
cboBox.ValueMember = "ID"                        'Field name that contains the Values you want associated with the displayed item

0
 
LVL 17

Expert Comment

by:ZeonFlash
ID: 17082523
ptakja beat me :).  Databinding really is the best option for this case, as it allows you to use the built-in methods of the combobox class, such as .SelectedValue.  But if you're dead set on doing it manually, then I'd suggest these links:
http://www.vbcity.com/forums/topic.asp?tid=47804
http://www.thescarms.com/dotnet/Combobox.asp
0
 

Author Comment

by:jackjohnson44
ID: 17082638
I don't mind databinding, but I have 2 followups.

1.  How would I handle an alphabetical sort on the description column?

2.  How would I add a first row to the combo box such as "Please Select" or "Add New" with an index of say "-1" or some other number?

Thanks
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 17

Assisted Solution

by:ZeonFlash
ZeonFlash earned 300 total points
ID: 17082775
1.  Do you mean you wish to sort the column before or after you bind to the combobox?

2.  You would have to add the row to the dataset and then bind it to the combobox:

        Dim dr As DataRow = table.NewRow

        dr.Item("ID") = -1
        dr.Item("description") = "Please Select..."
        table.Rows.InsertAt(dr, 0)
0
 

Author Comment

by:jackjohnson44
ID: 17083222
Thanks for #2

I don't really understand how you could sort after.
Can you please explain and answer both scenarios?

Thanks
0
 
LVL 17

Assisted Solution

by:ZeonFlash
ZeonFlash earned 300 total points
ID: 17084335
Well I guess it depends on what the purpose of sorting is.  Are you sorting the list before binding it to the combobox, therefore ending up with a sorted list of options?  If so, as long as you insert the blank row before you bind the data, everything should work out fine.

Or are you going to want to sort the list dynamically?  For example, you might have 2 radio buttons (Ascending and Descending), that would sort the items in the combobox based on the selected radio button.  In that case, you would need to use a DataView to sort and re-bind the data.  However, since your "Please Select" row might be shuffled around because of the sorting, you'd need to remove it before sorting and add it back to the top afterwards

        Dim dvMyData As New DataView(table)

        'Remove the "Please Select" row, which should be at the top of the table (index 0).
        dvMyData.Table.Rows.RemoveAt(0)

        'use ASC or DESC to sort the named columns
        dvMyData.Sort = "ID ASC"          

        'Add the "Please Select" row here

        cmbBox.DisplayMember = "description"
        cmbBox.ValueMember = "ID"
        cmbBox.DataSource = dvMyData
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 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

10 Experts available now in Live!

Get 1:1 Help Now