Home All Groups Group Topic Archive Search About

How to convert a selectedIndex to SelectedValue for ComboBox

Author
16 Jan 2006 4:37 PM
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

Author
16 Jan 2006 5:25 PM
Morten Wennevik
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();



On Mon, 16 Jan 2006 17:37:42 +0100, Mark L. Breen <mark.breen@nospam-gmail.com.off> wrote:

Show quoteHide quote
> 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
>
>
>
>



--
Happy coding!
Morten Wennevik [C# MVP]
Author
17 Jan 2006 2:57 AM
Jim Wooley
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

Show quoteHide quote
"Mark L. Breen" <mark.breen@nospam-gmail.com.off> wrote in message
news:OGyMusrGGHA.3056@TK2MSFTNGP09.phx.gbl...
> 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.
Author
18 Jan 2006 5:47 AM
Earl
intValue = cbo.SelectedValue(cbo.SelectedIndex)

Show quoteHide quote
"Mark L. Breen" <mark.breen@nospam-gmail.com.off> wrote in message
news:OGyMusrGGHA.3056@TK2MSFTNGP09.phx.gbl...
> 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
>
>
>
Author
15 Feb 2006 9:10 PM
Cerebrus99
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.

Show quoteHide quote
"Mark L. Breen" <mark.breen@nospam-gmail.com.off> wrote in message
news:OGyMusrGGHA.3056@TK2MSFTNGP09.phx.gbl...
> 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
>
>
>