Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi
I hope you can help me to know the difference between these two sentences
C#
something=((DataRowView)listBox1.SelectedItem).Row.ItemArray[0].ToString();

and
C#
something = listBox1.SelectedValue.ToString();
Posted

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.
 
Share this answer
 
Comments
TageTage 14-May-12 12:04pm    
thank you all..
that was helpful..
((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.
 
Share this answer
 
Comments
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...

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

 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900