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

Populating a string from the selected items of a listview

P: n/a
I have a listview in my application which now enables multiple entries
to be selected. I now need to put the value of the selected entries
into a string to be used for reporting purposes.

I use the following syntax but keep getting the error "Specified cast
is not valid". I don't know what's causing this ? Can anybody help ?

For Each Item As ListViewItem In
frmRepParams.lstAnalysis.SelectedItems
If Len(strAnalysis) = 0 Then
strAnalysis = Item.Text
Else : strAnalysis = strAnalysis & ", " &
frmRepParams.lstAnalysis.SelectedValue

End If
Next
Nov 21 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Should the Else part be like this?

strAnalysis = strAnalysis & ", " & Item.Text

"Robert Zirpolo" <ro******@hotmail.com> wrote in message
news:2d**************************@posting.google.c om...
I have a listview in my application which now enables multiple entries
to be selected. I now need to put the value of the selected entries
into a string to be used for reporting purposes.

I use the following syntax but keep getting the error "Specified cast
is not valid". I don't know what's causing this ? Can anybody help ?

For Each Item As ListViewItem In
frmRepParams.lstAnalysis.SelectedItems
If Len(strAnalysis) = 0 Then
strAnalysis = Item.Text
Else : strAnalysis = strAnalysis & ", " &
frmRepParams.lstAnalysis.SelectedValue

End If
Next
Nov 21 '05 #2

P: n/a
In the Else part, you could just do:
strAnalysis = strAnalysis & ", " & Item.Text

Btw, if lstAnalysis is a listview control, SelectedValue shouldn't work
since its not a member of the listview control but a member of the
ListControl from which the ListBox and ComboBox are derived.

hope this helps..
Imran.

"Robert Zirpolo" <ro******@hotmail.com> wrote in message
news:2d**************************@posting.google.c om...
I have a listview in my application which now enables multiple entries
to be selected. I now need to put the value of the selected entries
into a string to be used for reporting purposes.

I use the following syntax but keep getting the error "Specified cast
is not valid". I don't know what's causing this ? Can anybody help ?

For Each Item As ListViewItem In
frmRepParams.lstAnalysis.SelectedItems
If Len(strAnalysis) = 0 Then
strAnalysis = Item.Text
Else : strAnalysis = strAnalysis & ", " &
frmRepParams.lstAnalysis.SelectedValue

End If
Next

Nov 21 '05 #3

P: n/a
Apologies, your right.

But this is not the issue, it fails at the point it attempts to populate
the string. The code still sails with an "Specified cast not valid"
error.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #4

P: n/a
Robert,

Probably the part you miss is this one
frmRepParams.lstAnalysis.SelectedValue.ToString

I hope this helps?

Cor
Nov 21 '05 #5

P: n/a
No luck, using the following syntax still gives the same 'specified cast
is not valid'

For Each Item As ListViewItem In
frmRepParams.lstAnalysis.SelectedItems()

If Len(strAnalysis) = 0 Then strAnalysis =
frmRepParams.lstAnalysis.SelectedValue.ToString

Else : strAnalysis = strAnalysis & ", " &
frmRepParams.lstAnalysis.SelectedValue.ToString

End If

Next

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #6

P: n/a
I have to assume a lot with this code of you.

I assume that frmRepParams is a globaly defined form that is created in this
class.

I assume that the lstAnalysis is surely loaded and selected before this is
done.
(Otherwise the Value is Nothing).

Is this true?

Cor
No luck, using the following syntax still gives the same 'specified cast
is not valid'

For Each Item As ListViewItem In
frmRepParams.lstAnalysis.SelectedItems()

If Len(strAnalysis) = 0 Then strAnalysis =
frmRepParams.lstAnalysis.SelectedValue.ToString

Else : strAnalysis = strAnalysis & ", " &
frmRepParams.lstAnalysis.SelectedValue.ToString

End If

Next

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 21 '05 #7

P: n/a
Yes give me some credit.

Basically I am trying to loop through the selected i.e. highlighted
items in the Listbox I have created.

Can I use the For Each loop with a listbox. I am concerned if
ListViewItem is correct in this scenario ?

I have declared a valid string and just wish to populate this with the
values of the selected items.

Any help on how to do this would be useful, an example of code that
works would be good.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #8

P: n/a
Robert,

Can I use the For Each loop with a listbox. I am concerned if
ListViewItem is correct in this scenario ?

Surrely not and I never know how to handle this with a for each loop,
however maybe this quick sample i made can solve your problem. Because it
gives the same result. (This is a version without databinding however should
work the same)
\\\\
Dim MySelectedItems(Me.ListBox1.SelectedItems.Count) As String
For i As Integer = 0 To Me.ListBox1.SelectedItems.Count - 1
MySelectedItems(i) = Me.ListBox1.SelectedItems(i).ToString
Next
////
I hope this helps?

Cor
Nov 21 '05 #9

P: n/a
Ok no syntax error occurs but how can I use the MySelectedItems ? I can
see you have declared this as a string but I don't know how I can use
this as I need to pass it into another function as a parameter ?

When querying the info in MySelectedItems the info it acme back with is,

? myselecteditems
{Length=4}
(0): "System.Data.DataRowView"
(1): Nothing
(2): Nothing
(3): Nothing

I was expecting it to be a string with data like "1234".

We are nearly there.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #10

P: n/a
Robert,

I know it again with the for each assuming you have binded it to a datatable
\\\
For Each drv As DataRowView In ListBox1.SelectedItems
Dim mydate As Date = CDate(drv("MydateValue"))
Next
///

I hope this helps?

Cor
Nov 21 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.