Solved

Loading a ComboBox based off of another ComboBox's SelectedValue?

Posted on 2014-10-08
11
80 Views
Last Modified: 2014-10-09
This question is sort of related to a prior question; "Throwing an exception on a SelectedIndex property?"

This is going to be difficult to explain.

Here is my scenario. I have 2 ComboBoxes on my Windows Form, "Unit Type (cmbCapacityUnitType)" and "Unit" (cmbCapacityUnit). In the Form_Load Event, I run a routine that binds the "Unit Type" combobox to a DataTable (See Illustration 1). Both CB's reside on a TabControl within another TabControl.

When I'm adding a new record, the functionality works as it should. The problem occurs when I'm in edit mode. The user is able to select a record from a DataGridView. One of the columns in the DGV contain the "Unit Type" i.e. "Mass/Weight". I use the DoubleClick Event of the DGV to load the data onto the form. I execute the following statement to locate the "Mass/Weight" in the cmbCapacityUnitType:

            idx = cmbCapacityUnitType.FindString(row.Cells(27).Value, 1)
            cmbCapacityUnitType.SelectedIndex = idx
            stcComps.capacityUnitType = cmbCapacityUnitType.Text

            idx = cmbCapacityUnit.FindString(row.Cells(9).Value, 1)
            cmbCapacityUnit.SelectedIndex = idx
            stcComps.capacityUnit = cmbCapacityUnit.Text

Open in new window


The first 3 lines of the code above fine. However, when this statement: cmbCapacityUnitType.SelectedIndex = idx is executed, it triggers the Event for the that control (See Illustration 3). At this point, the cmbCapacityUnitType.SelectedValue is 17, which it should be. So...it goes and executes the LoadCombo_Detail_Units routine.

At this point, the DataTable contains 69 rows. When the DataTable is assigned to the cmbCapacityUnit.DataSource, the cmbCapacityUnit.Items.Count = 0.

The above scenario only happens on the first selection of a record from the DGV. All subsequent row selections will  return the correct data. It only happens on the first selection. I personally think this might have to do with the fact that the CB's reside on TabControls but fail to see the correlation.

I hope this makes sense.

    Illustration 1

    Private Sub LoadCombo_UnitType(ByVal iUnitType_ID As Integer)
        Try
            InitializeErrorClass(EH)
            BL.LoadCombo_UnitTypes(EH, 1)

            cmbCapacityUnitType.DataSource = Nothing

            If EH.ErrorMessage = "" Then
                If EH.DataSet.Tables(0).Rows.Count > 0 Then
                    cmbCapacityUnitType.DataSource = EH.DataSet.Tables(0)
                    cmbCapacityUnitType.ValueMember = EH.DataSet.Tables(0).Columns(0).ColumnName
                    cmbCapacityUnitType.DisplayMember = EH.DataSet.Tables(0).Columns(1).ColumnName
                    cmbCapacityUnitType.SelectedIndex = -1
                End If
            End If

        Catch ex As Exception
            EH.ErrorMessage = "LoadCombo_Manufacturers() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try
    End Sub

Open in new window


    Illustration 2

    Private Sub LoadCombo_Detail_Units(ByVal iUnitType_ID As Integer)
        Try
            InitializeErrorClass(EH)
            BL.LoadCombo_UnitsByUnitType(iUnitType_ID, EH)

            cmbCapacityUnit.DataSource = Nothing

            If EH.ErrorMessage = "" Then
                If EH.DataSet.Tables(0).Rows.Count > 0 Then
                    cmbCapacityUnit.DataSource = EH.DataSet.Tables(0)
                    cmbCapacityUnit.ValueMember = EH.DataSet.Tables(0).Columns(0).ColumnName
                    cmbCapacityUnit.DisplayMember = EH.DataSet.Tables(0).Columns(1).ColumnName
                    cmbCapacityUnit.SelectedIndex = -1
                End If
            End If

        Catch ex As Exception
            EH.ErrorMessage = "LoadCombo_Detail_Units() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try
    End Sub

Open in new window


    Illustration 3

    Private Sub cmbCapacityUnitType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbCapacityUnitType.SelectedIndexChanged
        Try
            EH.ErrorMessage = ""

            If Not blnIsLoading Then
                If cmbCapacityUnitType.SelectedIndex > 0 Then
                    iCapacityUnitType_ID = cmbCapacityUnitType.SelectedValue
                    LoadCombo_Detail_Units(iCapacityUnitType_ID)
                    cmbCapacityUnit.Focus()
                End If
            End If

        Catch ex As Exception
            EH.ErrorMessage = "cmbCapacityUnitType_SelectedIndexChanged() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
    End Sub

Open in new window

0
Comment
Question by:BlakeMcKenna
  • 6
  • 5
11 Comments
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 40370301
0
 

Author Comment

by:BlakeMcKenna
ID: 40370631
Eric,

Great article and I can see the benefit of the 3rd solution. However, the first issue is, I'm working in VB.Net and I've never seen the "cShared.CreatedDataSet()". I'm going to assume that it's the same the following statement:

Dim ds As New DataSet

Open in new window


If my assumption is wrong, how do I get the cShared Object?

The second issue is where does the DataSet get the data? When I ran my code, I got the "Object reference not set to an instance of an object" error. The table names "LKUP_UnitTypes" and "LKUP_Units" are the actual table names  in my SQL Server DB. Just looking at that code...I don't see how or where the dsUnitTypeUnits is being populated. Does creating the relationship do that automatically?

My Parent CB is called "cmbCapacityUnitType". My Child CB is called "cmbCapacityUnit".

I've attached screenshots of my code. They are labeled Step 1 and Step 2. I tried Step 1 first with no luck, then I tried Step 2 with no luck.
Step-1.jpg
Step-2.jpg
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 40370699
>>If my assumption is wrong, how do I get the cShared Object?

Download the demo code. It is a method that creates a dataset containing 2 datatables and some test data.
0
 

Author Comment

by:BlakeMcKenna
ID: 40370701
I forgot to add the routine that populates the dsUnitTypeUnits.  It's actuall an image of SSMS and the query that creates the data as well as the data itself.
Step-3.jpg
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 40370725
play with the downloadable demo to see them working.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:BlakeMcKenna
ID: 40370807
I posted my last message before I read your last post. I'm working on it now!

Thanks Eric!
0
 

Author Comment

by:BlakeMcKenna
ID: 40371060
Hi Eric,

I have tried every thing I can think of based off of your code and I can't get the CB's to populate. I have attached all the code in a Text file. It's not a lot of code but I would sure be grateful if you could see my problem.

Thanks!
MyCode.txt
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 40371141
have you been able to make my demo to work?

have you put a breakpoint in the CapacityUnitRelationship method? do you completely go through?
0
 

Author Comment

by:BlakeMcKenna
ID: 40371211
I did get your demo to work. I did put breakpoints in the CapacityUnitRelationship and stepped thru every line of code. Both tables contain the data that they are suppose to since I dynamically loaded them as opposed to manually loading them as your demo app did.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 40371270
not sure what it could be. you should normally get at least the first one filled. Are you sure of your field name?

maybe try solution 2 which is easier to set up
0
 

Author Closing Comment

by:BlakeMcKenna
ID: 40371330
I finally got it working Eric!

Thanks again!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
scanning dentists xray (the small ones) 3 43
Help with Syntax 9 25
VS.net 2010 11 18
Handle KeyDown for ToolStripDropDownButton, ToolStripMenuItem 3 18
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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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