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
Morten Wennevik - 16 Jan 2006 17:25 GMT
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[i];
textBox1.Text = r["Col2"].ToString();
> Hello All,
>
[quoted text clipped - 20 lines]
>
> Mark

Signature
Happy coding!
Morten Wennevik [C# MVP]
Jim Wooley - 17 Jan 2006 02:57 GMT
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
> What I want to be able to do is retrieve the Selected Value for a given
> Selected Index
> But this does not always seem to work and sometimes I find that the
> selected value is null.
Earl - 18 Jan 2006 05:47 GMT
intValue = cbo.SelectedValue(cbo.SelectedIndex)
> Hello All,
>
[quoted text clipped - 20 lines]
>
> Mark
Cerebrus99 - 15 Feb 2006 21:10 GMT
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.
> Hello All,
>
[quoted text clipped - 20 lines]
>
> Mark