How to convert a selectedIndex to SelectedValue for ComboBox

  • Thread starter Thread starter Mark L. Breen
  • Start date Start date
M

Mark L. Breen

Hello All,

I have a selected index for a combo, which I percieve to be the best value
to use when programitically maniplating combos.

What I want to be able to do is retrieve the Selected Value for a given
Selected Index

It may be just me, but I have found the whole selected value, selected text
and selectindex to be troublesome to work with.

Is there a recommend way to retrive the Selected Value?

The way I am currently have to do it it

cbo.SelectedIndex = intIndex
intValue = cbo.SelectValue

But this does not always seem to work and sometimes I find that the selected
value is null.

Any advice would be appriciated, thanks

Mark
 
Hi Mark,

You could use the Item indexer of the ComboBox, but you would then need to cast it to whatever type you put there. For instance, in case of using a DataTable as DataSource, the ComboBox will be full of DataRowViews

DataRowView r = (DataRowView)comboBox1.Items;
textBox1.Text = r["Col2"].ToString();
 
If no item is selected, or an invalid text combination is entered, no Item
will be selected, thus your SelectedValue will be Nothing/Null. Note:
SelectedIndexChanged does not fire when no item is selected even if a
previous version was selected.

Additionally, if binding the value to a String Property, the value parameter
of the Setter will be Nothing/Null even if the property is of the simple
String type (not a nullable string). Hence another reason to check your
input parameters for all strings.

Jim
 
Hi Mark,

Nothing to it, if I understand you correctly. (You want to get the value of
the currently selected item in your combobox ?)

Dim idx as integer = cbo.SelectedIndex
if idx <> -1 'If an item is selected
Msgbox(cbo.Items(idx).ToString())
End if

Regards,
Cerebrus.
 
Back
Top