Your technology certification is waiting. Enroll in Cloud Class ®
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
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
Please enter a first name
Please enter a last name
Must be at least 4 characters long.
Join and Comment
From novice to tech pro — start learning today.
Premium members can enroll in this course at no extra cost.