Get Selected Items from Listbox...

cboshdave

Member
Joined
Dec 11, 2014
Messages
24
Programming Experience
1-3
I am trying to pull the selected values from a listbox. Seems simple on the surface. There are several examples referring to the following:

C#:
   foreach (var selecteditem in listBoxDocStatus.SelectedItems)        {
           Debug.WriteLine("Selected Item is: " + selecteditem.ToString());
        }

When I run this code, I see the following: "Selected Item is: System.Data.DataRowView"

I'm not sure how to access the actual index or value? I see that there is an array attached: 0 is index and position 1 is value. I need 0 but I don't know how to pull it.

Thanks,
Dave
 
When you bind a list to a ListBox, ComboBox or the like, the default behaviour is for the control to call the ToString method on each item to get text to display for that item. You can override that behaviour by setting the DisplayMember property of the control to the name of the property or column of each item you want to display.

It appears that you have bound a DataTable to your ListBox. When you do that, the data actually comes from the DefaultView of the DataTable, which is a DataView and contains DataRowView items. You have presumably set the DisplayMember in order to display data from a particular column but the code you posted doesn't take that into account. It simply calls ToString on each item and therefore you get the result of DataRowView.ToString, which is just the name of the type, as it is for most complex types.

The correct way to go about this is like so:
foreach (var selecteditem in listBoxDocStatus.SelectedItems) {
    Debug.WriteLine("Selected Item is: " + listBoxDocStatus.GetItemText(selecteditem));
}
That will do just as the ListBox itself does to get display text, i.e. it will use the DisplayMember if it is set, otherwise it will fall back to calling ToString.
 
That is a perfect answer! Worked great and makes sense. Thank you! and I appreciate the thought about the index access. That is indeed what I needed! Saved me posting back!
 
Just one more question and a clarification.
Clarification: How do I mark an answer as "Answered"? I don't see that option.
Question: If I want to ADD an option to my listbox, i could not bind it to my datatable (results of my query - my current approach). I assume I would need to run the query, load the list whiling thru the query results, and then just add one more option at the end. I am pretty sure this is the right approach, just wanted to confirm. Then I would go back to the other way of pulling the results.
 
How do I mark an answer as "Answered"? I don't see that option.
When starting a thread there is a Prefix drop-down. If you're asking a question, you should select Question from that list. Once you're question is answered, you can edit the first post, hit the Go Advanced button and then select Answered from that drop-down.
If I want to ADD an option to my listbox, i could not bind it to my datatable (results of my query - my current approach). I assume I would need to run the query, load the list whiling thru the query results, and then just add one more option at the end. I am pretty sure this is the right approach, just wanted to confirm. Then I would go back to the other way of pulling the results.
You can only bind to a data source or add items directly but not both. If the control is bound and you want an extra item then yes, you must add that item to the data source. If the data source is a DataTable then that means manually adding a new row to that DataTable.
 
Hi Everyone
I ran into a similar problem. Try to create a string fron the items of the listbox.
This is my code
string[] items = listBox1.Items.OfType<object>().Select(item => item.ToString()).ToArray();
string result = string.Join(",", items);
label1.Text = result;

Problem: As cboshdave described I get only System.Data.DataRowView over and over.
The solution jmcilhinney gave is very good but how can i change it to recieve all the items, not only the selected
thanks in advance
 
Hi Everyone
I ran into a similar problem. Try to create a string fron the items of the listbox.
This is my code
string[] items = listBox1.Items.OfType<object>().Select(item => item.ToString()).ToArray();
string result = string.Join(",", items);
label1.Text = result;

Problem: As cboshdave described I get only System.Data.DataRowView over and over.
The solution jmcilhinney gave is very good but how can i change it to recieve all the items, not only the selected
thanks in advance

In your code, this is the part that gets the text for an item:
C#:
items = listBox1.Items.OfType<object>().Select(item => [B][U]item.ToString()[/U][/B]).ToArray();
You simply replace that with the code that I provided to get the text for an item:
C#:
items = listBox1.Items.OfType<object>().Select(item => [B][U]listBox1.GetItemText(item)[/U][/B]).ToArray();
 
Back
Top Bottom