?
Solved

vb.net combobox add with index

Posted on 2006-07-11
6
Medium Priority
?
863 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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
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…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

840 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