Click here to Skip to main content
11,640,125 members (65,738 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF C#4.0
Hi
I hope you can help me to know the difference between these two sentences
something=((DataRowView)listBox1.SelectedItem).Row.ItemArray[0].ToString();

and
something = listBox1.SelectedValue.ToString();
Posted 14-May-12 3:35am
Rate this: bad
good
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.
  Permalink  
Comments
TageTage at 14-May-12 12:04pm
   
thank you all..
that was helpful..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

((DataRowView)listBox1.SelectedItem).Row.ItemArray[0].ToString();
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.
  Permalink  
Comments
Shmuel Zang at 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
good
Please Sign up or sign in to vote.

Solution 2

For the difference between SelectedValue and SelectedItem, you can read this link: http://bea.stollnitz.com/blog/?p=9

  Permalink  

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

  Print Answers RSS
0 Suvendu Shekhar Giri 215
1 Mika Wendelius 188
2 CPallini 167
3 Sergey Alexandrovich Kryukov 139
4 OriginalGriff 125


Advertise | Privacy | Mobile
Web01 | 2.8.150731.1 | Last Updated 14 May 2012
Copyright © CodeProject, 1999-2015
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