[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

vb.net combobox add with index

Posted on 2006-07-11
6
Medium Priority
?
860 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 14

Accepted Solution

by:
ptakja earned 800 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 17

Assisted Solution

by:ZeonFlash
ZeonFlash earned 1200 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 1200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

656 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