Solved

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

Posted on 2014-10-08
11
78 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
Comment Utility
0
 

Author Comment

by:BlakeMcKenna
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
play with the downloadable demo to see them working.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

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

Thanks Eric!
0
 

Author Comment

by:BlakeMcKenna
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I finally got it working Eric!

Thanks again!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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 …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

10 Experts available now in Live!

Get 1:1 Help Now