Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Pulling a value from a databound (lookup table) combobox? How?

Posted on 2004-04-21
12
Medium Priority
?
415 Views
Last Modified: 2010-04-24
I have a Windows Form application that has several ComboBox controls on it. (Let's just deal with one for now). My SQL database has several lookup tables related to products and versions of our software, something like this (fake - for example only):

Product_ID   Product_Name
5                 Word
10               Excel
16               Access

So I bind the ComboBox control to the data like this:
        'Populate the Product Dropdown
        cmbProduct.DataSource = Database.GetProducts.Tables(0)  'Database is a class, the GetProducts Function returns a DataSet
        cmbProduct.DisplayMember = "Product_Name"
        cmbProduct.ValueMember = "Product_ID"

Great! So now my ComboBox is loaded and displaying the names of the products. This works just fine.

Now, I have a second combobox, that lists the versions of a given product. It might look like this:

Version_ID   Product_ID    Version Name
1                 5                  1.0
2                 5                  2.0
3                 10                1.0.1
4                 10                1.0.2
5                 16                2.0
6                 16                3.0

I need to pull the Versions by supplying the Product_ID from the Product Combobox.  (i.e. SELECT * FROM Versions WHERE Product_ID = 5)
For example:
    Private Sub cmbProduct_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbProduct.SelectedIndexChanged
        'Populate the Version Dropdown
        If cmbProduct.SelectedIndex <> -1 Then
            cmbVersion.DataSource = Database.GetProductVersions(CType(cmbProduct.SelectedValue, Integer)).Tables(0)
            cmbVersion.DisplayMember = "Product_Name"
            cmbVersion.ValueMember = "Product_ID"
        End If
    End Sub

OK, SO HERE IS MY PROBLEM.

I can't figure out how to pull the "selected value" from the Combobox. There is indeed a property called .SelectedValue, but it returns a System.Object, and when I try to do a CTYPE(objCombo.SelectedValue, Integer), it throws an error, "Cast from type 'DataRowView' to type 'Integer' is not valid."

All I really want to know is what value Product_ID is related to the selected item. I suppose I can pull the ComboBox.Text value and jump through hoops to reverse engineer that value back out, but that seems to defeat the purpose. Sorry guys, I'm used to ASP.NET where I can just request the SelectedItem.Value and it gives it to me.

Any help would be greatly appreciated.
0
Comment
Question by:toddhd
11 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 10880287
I guess that you could cast the SelectedValue to a DataRowView, and then get the Row property:

Dim rowCurrent As DataRow = CType(cmbVersion.SelectedValue, DataRowView).Row
Dim productID As Integer = CType(rowCurrent("Product_ID"), Integer)

Bob
0
 
LVL 8

Author Comment

by:toddhd
ID: 10881788
Ok, here is what I learned:

The code above from "TheLearnedOne" works - but - only once.

Here is the deal - I call the subroutine to initially load the comboboxes during the Page_Load event. When the cmbProduct combobox loads, it fires off the SelectedIndexChanged event (as it should). That causes the "invalid cast" error, as the SelectedValue is considered to be a DataRow. So I added the lines above.

Now the program boots up ok, but if the users clicks on the Products Combobox, then it errors out again! NOW it no longer considers the SelectedValue to a Datarow, instead, it is a value (like you'd expect it to be). So here is the breakdown:

During the Form_Load event, SelectedValue is a Datarow
After the Form_Load event, it is not a datarow (my guess is that it is a listitem?)

So, here is the SelectedIndexChanged Event that works:

    Private Sub cmbProduct_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbProduct.SelectedIndexChanged
        'Populate the Version Dropdown
        If cmbProduct.SelectedIndex <> -1 Then
            Try
                cmbVersion.DataSource = Database.GetProductVersions(CInt(cmbProduct.SelectedValue)).Tables(0)
                cmbVersion.DisplayMember = "Version_Name"
                cmbVersion.ValueMember = "Version_ID"
            Catch ex As Exception
                Dim rowCurrent As DataRow = CType(cmbProduct.SelectedValue, DataRowView).Row()
                Dim productID As Integer = CType(rowCurrent("Product_ID"), Integer)
                cmbVersion.DataSource = Database.GetProductVersions(productID).Tables(0)
                cmbVersion.DisplayMember = "Version_Name"
                cmbVersion.ValueMember = "Version_ID"
            End Try
        End If
    End Sub

I of course have NO IDEA why the object would be different during the Form_Load event.
0
 

Expert Comment

by:poppyclose
ID: 10887988
Try rearranging your code like so:

            cmbVersion.DisplayMember = "Product_Name"
            cmbVersion.ValueMember = "Product_ID"
            cmbVersion.DataSource = Database.GetProductVersions(CType(cmbProduct.SelectedValue, Integer)).Tables(0)

Set your Display and Value Members before setting the datasource.
You should now be able to use cmbVersion.SelectedValue
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 8

Author Comment

by:toddhd
ID: 10888378
Sorry poppyclose, same problem. During Form_Load, it bombs on the Datarow issue. Good thinking however, thanks for trying.
0
 
LVL 2

Expert Comment

by:navneet77
ID: 10888958
Hi toddhd,

Try to type cast the selecteditem to datarowview instead of selectedvalue.

That should do the trick. You should also put a break point and see that during form load is there anything in selecteditem of the first combobox because depending on how you put the code it might be possible that there is nothing selected in the first combobox during form load.

Hope it help

Navneet
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11094364
Are you still having a problem with this?

Bob
0
 
LVL 8

Author Comment

by:toddhd
ID: 11098302
Yes and no. None of the answers posted worked (except my workaround using Try/Catch). To be honest, the code has changed since this question was posted, and it is no longer an issue for me personally.

However, I'm not sure what to do with the post. I hate to delete it, as someone may benefit from it in the future, or perhaps even find the correct answer. But since none of the answers worked, it doesn't make sense to "accept" one.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11098348
You can always post a question in Community Support and ask for the question to be marked as a (P)reviously (A)sked (Q)uestion, so the question can be kept, but you can get your points back.

Bob
0
 
LVL 8

Author Comment

by:toddhd
ID: 11099171
Ah, cool, thanks for the info!
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11099191
No problem, glad to be of service.

Bob "Cleanup Volunteer"
0
 
LVL 1

Accepted Solution

by:
GhostMod earned 0 total points
ID: 11147443
PAQd, 125 points refunded.

GhostMod
Community Support Moderator
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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 …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview
Suggested Courses

824 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