Reading multiple values from a ListBox

P

Pippen

Hi,

I'm trying to read all the selected items in a ListBox. So after the user
selects the values they want in the ListBox they select a button. This is
where I want to put into a string all the values that were selected. The
ListBox is populated by a stored proc that reads a DB table. Yes, I have the
SelectionMode property set to MultiExtended, I have also tried MultiSimple.

I have seen some examples for ASP Web forms but I'm using Windows Forms
(Stand alone Application).

I have tried:

string msg = "";

for (int i = lstData.Items.Count - 1; i >= 0; i--)
{
if(lstData.GetSelected(i) == true)
{
// do something here
}
}


In the // do something here part I have tried the following:

msg = lstData.Items.ToString(); - I get "Sytem.Data.DataRowView" string
for each Selected item in the ListBox

msg = lstData.SelectedValue.ToString(); - This returns the correct value but
only for the first on and the same value is repeated. Does not work for the
second or greater value selected in ListBox.

I have tried others but these seemed the most promising. Please show me how
to read the Value of the ListBox for multiple selections. The Value I think
is what I want not the Displayed Text.

Any help would be greatly apprecaited,

Thanks,

-p
 
J

Jon Skeet [C# MVP]

In the // do something here part I have tried the following:

msg = lstData.Items.ToString(); - I get "Sytem.Data.DataRowView" string
for each Selected item in the ListBox


Instead of just calling ToString(), cast to a DataRowView and then
extract the part of the row that you're interested in.

Jon
 
P

Pippen

Jon Skeet said:
In the // do something here part I have tried the following:

msg = lstData.Items.ToString(); - I get "Sytem.Data.DataRowView"
string
for each Selected item in the ListBox


Instead of just calling ToString(), cast to a DataRowView and then
extract the part of the row that you're interested in.

Jon


That worked great! Thank you for your help.

-p
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top