Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

Anyone please let me know how to get the selected value and item from a data bound ListBox

Please help me, Thanks in advance.

XAML code

XML
<ListBox x:Name="listbox1" Width="150" ItemsSource="{Binding Things}" DisplayMemberPath="department_name" SelectedValuePath="department_id" Margin="12,35,326,12"  />


C#
Data binding with Dataset

C#
listbox1.DisplayMemberPath = "department_name";
listbox1.SelectedValuePath = "department_id";
listbox1.ItemsSource = ds.Tables[0].DefaultView;



Output

F#
MessageBox.Show(listbox1.Items.CurrentItem.ToString());//Output -System.Data.DataRowView
MessageBox.Show(listbox1.SelectedItem.ToString());////Output -System.Data.DataRowView
MessageBox.Show(listbox1.SelectedValue.ToString());//Object reference not set to an instance of an object.
Posted
Updated 6-Jan-15 0:25am
v2
Comments
Arjsrya 6-Jan-15 6:38am    
Did you try this?

listbox1.SelectedItem.Value
Znaneswar K 6-Jan-15 6:44am    
Yeah i tried but its not working.
Christian Amado 6-Jan-15 6:50am    
Have you got a Selected item in your listbox? Did you selected any item in your listbox?
Znaneswar K 6-Jan-15 6:53am    
Yeah i have selected the item.
I have tried this one at listbox1_SelectionChanged event and also at onMouseClick.
Znaneswar K 6-Jan-15 6:55am    
listbox1.SelectedIndex working fine. But i need selectedvalue and selecteditem.

1 solution

Hi kznaneswar, Have you got any selected item in your ListBox?
C#
MessageBox.Show(listbox1.SelectedValue != null ? listbox1.SelectedValue.ToString() : "I have 0 selected item!"); // If the selected value is null print a custom message.

Edit(based on your comment).-
Inside your Selection_Changed event write this:
C#
if (listbox1.SelectedItem!=null)
{
    MessageBox.Show(listbox1.Items.CurrentItem.ToString());//Output -System.Data.DataRowView
    MessageBox.Show(listbox1.SelectedItem.ToString());////Output -System.Data.DataRowView
    MessageBox.Show(listbox1.SelectedValue != null ? listbox1.SelectedValue.ToString() : "I have 0 selected item!");
}

Another approach. This going to work!
C#
if (listbox1.SelectedItem!=null)
{
    DataRowView d1=listbox1.SelectedItem as DataRowView;
    MessageBox.Show(d1["department_id"].tostring());
}


Hope it helps.
 
Share this answer
 
v3
Comments
Znaneswar K 6-Jan-15 7:12am    
Hi, Thanks a lot for your reply. I've tried the last approach, and I'm getting exception as "department_id is neither a DataColumn nor a DataRelation for table Table."
Christian Amado 6-Jan-15 7:15am    
Put the correct column there, based on the result of your DataSet. Is your DataSet return a value at least?
Christian Amado 6-Jan-15 7:16am    
I assume that "Things" is the name of the DataSet.

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