I'm pretty sure I fell back into my VB 6.0 safety zone and solved a issue with a hammer...I'd like to find a better way. I have a table of reference values and I save the primary key of the refence table as a foreign key on the database table with claim information. I sort the reference table by description so the foreign key values are in a random order. So when I use the foreign key to set the selected index it pulls up the wrong ComboBox entry. For example, look at the screen print, if "Hearing Loss" was selected than the value saved is "5". When I reload the screen and use "5" to set the selected index it will display "Poisoning" as the display because the is its placement when I loaded it. Here is where the hammer comes in. When I load the ComboBox I save the value off the database next to the sequential number of the entries being loaded in a List of:
Then when I load the ComboBox I do this:
That give me the correct entry.
The old VB 6.0 method I used doesn't work the same in .Net.
In the screen print the sequential count represents the ComboBox entry and the second column the primary key from the reference table (I know it really starts at zero). My issue was what is selection stored on the database doesn't match the selected indexes in the ComboBox. I used the List of to match them up.
Code:
While SQLReader.Read()
cboInjuryType.Items.Add(New ComboboxItems(SQLReader.Item("pkRefOSHAInjuryType"), SQLReader.Item("Description")))
InjuryTypeDetail.ComboBoxIndex = mAddItemCtr -------------------------------------------------------------------------------- sequential number of the entries being loaded
InjuryTypeDetail.DataBaseIndex = SQLReader.Item("pkRefOSHAInjuryType") --------------------------------------------------------------- Primary key of reference table.
InjuryTypeList.Add(InjuryTypeDetail)
mAddItemCtr = mAddItemCtr + 1
End While
Code:
For Each Me.InjuryTypeDetail In InjuryTypeList
If SQLReader.Item("fkRefOSHAInjuryType") = InjuryTypeDetail.DataBaseIndex Then
mInjuryDetailSelectedIndex = InjuryTypeDetail.ComboBoxIndex
Exit For
End If
Next
Code:
cboInjuryType.SelectedIndex = mInjuryDetailSelectedIndex
In the screen print the sequential count represents the ComboBox entry and the second column the primary key from the reference table (I know it really starts at zero). My issue was what is selection stored on the database doesn't match the selected indexes in the ComboBox. I used the List of to match them up.