Set cbo.selectedvalue using Value problem persists
I posted this question once already, but somebody responded with a solution that did not work and I suspect nobody else is bothering to look at the question because they think the problem was solved by the response. I could really use a response that actually works:
I have a combo box on a Windows form populated from a table looking something like this:
DISPLAYMEMBER VALUEMEMBER INDEX
Select Name 0 0
Name1 193 1
Name2 40 2
The user selects an Item from the cbo and the VALUE is saved to another table. When the user later edits the saved data, I want to automatically select the VALUE from the combobox to reflect the previous selection. The only way I have been able to select the item I want by VALUE is by looping and checking every VALUE to find the one I want:
For intPos As Integer = 0 To .Count - 1
If comboBox.Item(intPos)("Value") = v_intValue Then
comboBox.SelectedIndex = intPos
Exit Sub
End If
Next
It has to do this for over 40 comboboxes on the form, and as you can imagine, that's not exactly efficient. It's all the more frustrating that the ASP version of the combobox supports the Items.FindByValue method to determine the combobox index of the value in question (exactly what I want) and the windows control does not (Hello, Microsoft?!?!?!)
I've found sites claiming this will work, but it doesn't:
combobox.SelectedValue = 40
I know for a fact that VALUE 40 exists in the combobox, but if I query SelectedValue after attempting to set it, it returns NOTHING and SelectedIndex is still 0. I've also seen suggestions that I try this:
combobox.SelectedIndex = combobox.FindString("40")
But FindString appears to only search the DISPLAYMEMBER, and not VALUEMEMBER, and the code above returns -1. Another dead end.
Finally, the solution suggested to my last posting of this question does not work because the method suggested doesn't even exist:
combobox.Value = 40
This seems like a pretty common need (and it's been implemented properly in ASP). Isn't there a more efficient way of selecting a combobox item using using VALUEMEMBER in a Windows form?
|