469,363 Members | 2,578 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,363 developers. It's quick & easy.

Reading multiple values from a ListBox

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
6 3903
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

"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

"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

"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

"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

"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.

Similar topics

reply views Thread by bill yeager | last post: by
2 posts views Thread by DC Gringo | last post: by
10 posts views Thread by ads | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.