Click here to Skip to main content
12,946,164 members (52,573 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I hope you can help me to know the difference between these two sentences

something = listBox1.SelectedValue.ToString();
Posted 14-May-12 3:35am
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Both your code lines are fine, assuming you've data bound to a dataview. The second line of code simply returns the "value" of the selected item (that is, whatever you've indicated as the ValueMember). The first reaches back into the data binding for your control, finds the row for the selected item, and returns the value in the first column--that value might or might not be the value or display member, depending on how you've coded it.
TageTage 14-May-12 12:04pm
thank you all..
that was helpful..
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Is this even CORRECT? I doubt that.

Listbox's selected item would be of type ListItem. You are trying to cast it into DataRowView. There should be cannot cast expression error.

Second one looks ok - a valid code line.
Shmuel Zang 14-May-12 10:33am
In addition to that, for avoiding NullReferenceException (on the second option), I would also check, if SelectedValue is null...
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

For the difference between SelectedValue and SelectedItem, you can read this link:


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 4,899
CHill60 3,205
Maciej Los 2,453
Jochen Arndt 1,935
ppolymorphe 1,795

Advertise | Privacy | Mobile
Web02 | 2.8.170518.1 | Last Updated 14 May 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100