473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Populating a string from the selected items of a listview

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
10 1668
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
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
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
Robert,

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

I hope this helps?

Cor
Nov 21 '05 #5
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: GTi | last post by:
I have a listview that with selected items. But I want to "reselect" items after a refresh. Each items have a uniqe value in the Tag object. After a refresh some new items may be added or removed...
7
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I'v been struggeling with removing selected items from a listview. Anyone that can give me a piece of code that does this? I am a newbee to this C# and cant figure it out.... Regards...
2
by: JP | last post by:
Hi, I am trying to loop through the listbox and read the selected items from the list, within a CLICK event on an aspx page. The following is what I have tried. It loops through the listbox,...
1
by: Peter | last post by:
How can I check which items has been selected in CheckBoxList control when I bind data to it from SQL Server? The code I use works fine when I add manually items into checkboxlist control, but does...
1
by: Robin Tucker | last post by:
Hi ppl, My owner draw list box controls do not "refresh" old selected items when a new selection is made. This means that as you click to make selections, the previously selected items stay...
3
by: ljlevend2 | last post by:
I've noticed that selected items in a ListView are only highlighted if the control has focus. I find this to be highly undesirable because it can cause the control to behave differently than what...
0
by: Brian Henry | last post by:
Since no one else knew how to do this I sat here all morning experimenting with this and this is what I came up with... Its an example of how to get a list of items back from a virtual mode list...
2
by: Doug | last post by:
Hi I have a datagridview in a windows C# application and i am allowing a user to select items from the datagridview. I allow the user to copy the selected items to the clipboard and then if...
1
by: Garudzo | last post by:
Hi all I am developning a small application in MS Access 2003 on a windows XP platform. I am using windows treeview and Listview controls. I have discovered that I can do a multi select of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.