By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,645 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,645 IT Pros & Developers. It's quick & easy.

Reading multiple values from a ListBox

P: n/a
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[i].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
Aug 9 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Aug 9, 8:07 am, "Pippen" <n...@valid.mailwrote:

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

msg = lstData.Items[i].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

Aug 9 '07 #2

P: n/a

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@r34g2000hsd.googlegr oups.com...
On Aug 9, 8:07 am, "Pippen" <n...@valid.mailwrote:

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

msg = lstData.Items[i].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
Aug 9 '07 #3

P: n/a

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@r34g2000hsd.googlegr oups.com...
On Aug 9, 8:07 am, "Pippen" <n...@valid.mailwrote:

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

msg = lstData.Items[i].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
Aug 9 '07 #4

P: n/a

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@r34g2000hsd.googlegr oups.com...
On Aug 9, 8:07 am, "Pippen" <n...@valid.mailwrote:

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

msg = lstData.Items[i].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
Aug 9 '07 #5

P: n/a

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@r34g2000hsd.googlegr oups.com...
On Aug 9, 8:07 am, "Pippen" <n...@valid.mailwrote:

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

msg = lstData.Items[i].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
Aug 9 '07 #6

P: n/a

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@r34g2000hsd.googlegr oups.com...
On Aug 9, 8:07 am, "Pippen" <n...@valid.mailwrote:

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

msg = lstData.Items[i].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
Aug 9 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.