Solved

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

Posted on 2014-10-08
11
83 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 70

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 70

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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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 70

Expert Comment

by:Éric Moreau
ID: 40370725
play with the downloadable demo to see them working.
0
 

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 70

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 70

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …

809 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