Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / February 2006

Tip: Looking for answers? Try searching our database.

How to convert a selectedIndex to SelectedValue for ComboBox

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mark L. Breen - 16 Jan 2006 16:37 GMT
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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.