Solved

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

Posted on 2004-04-21
12
400 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
12 Comments
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 8

Author Comment

by:toddhd
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Are you still having a problem with this?

Bob
0
 
LVL 8

Author Comment

by:toddhd
Comment Utility
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
Comment Utility
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
Comment Utility
Ah, cool, thanks for the info!
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
No problem, glad to be of service.

Bob "Cleanup Volunteer"
0
 
LVL 1

Accepted Solution

by:
GhostMod earned 0 total points
Comment Utility
PAQd, 125 points refunded.

GhostMod
Community Support Moderator
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now