Populating a string from the selected items of a listview

  • Thread starter Thread starter Robert Zirpolo
  • Start date Start date
R

Robert Zirpolo

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
 
Should the Else part be like this?

strAnalysis = strAnalysis & ", " & Item.Text

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
 
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.
 
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.
 
Robert,

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

I hope this helps?

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

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

Back
Top