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
Solved

vb.net combobox add with index

Posted on 2006-07-11
6
831 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need a starter for ETL protocol? 4 65
Web Form VB.Net  import CSV 4 43
Call an IPL program from vb.net and pass in a file location parmeter 2 27
ASP/VB email question 4 46
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…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

839 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