Solved

vb.net combobox add with index

Posted on 2006-07-11
6
844 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 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
Revamp Your Training Process

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

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

717 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