Experts Exchange Solution brought to you by
"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.
ComboBox SelectedItem, SelectedValue, SelectedWhat???
ComboBox has a lot of helpful properties that allow you to get access to items selected by the Control. The problem is knowing which one is best in which scenario. The documentation is not 100% clear on what the values will be in various situations. The most important factor to know is whether or not you are DataBinding in the ComboBox.
In this case the properties will have the following values
SelectedItem - For gets this will return the actual object in the DataSource that is being displayed in the ComboBox. For sets if the value exists in the DataSource, it will be selected, otherwise the operation will complete without an exception but won't actually do anything.
SelectedValue - This property depends on the value of ValueMember.
If the property ValueMember is not Nothing the ComboBox will look for a member on SelectedItem with the name specified in ValueMember and return that. This is also the value displayed in the ComboBox.
If the property ValueMember is Nothing, then the SelectedValue will return .ToString() on the SelectedItem
SelectedIndex - Index of SelectedItem in the DataSource
SelectedItem - Gets and Sets both go to the currently selected object from the Items collection
SelectedValue - Will be Nothing/null
SelectedIndex - Index in the Items collection of the SelectedItem
Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.
From novice to tech pro — start learning today.
Members can enroll in this course at no extra cost.