Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 89
  • Last Modified:

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

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
BlakeMcKenna
Asked:
BlakeMcKenna
  • 6
  • 5
1 Solution
 
BlakeMcKennaAuthor Commented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
>>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
Independent Software Vendors: 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!

 
BlakeMcKennaAuthor Commented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
play with the downloadable demo to see them working.
0
 
BlakeMcKennaAuthor Commented:
I posted my last message before I read your last post. I'm working on it now!

Thanks Eric!
0
 
BlakeMcKennaAuthor Commented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
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
 
BlakeMcKennaAuthor Commented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
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
 
BlakeMcKennaAuthor Commented:
I finally got it working Eric!

Thanks again!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now