Cast ListView Items to List<string>?
Asked Answered
I

4

17

How can I cast ListView.Items to a List<string>?

This is what I tried:

List<string> list = lvFiles.Items.Cast<string>().ToList();

but I received this error:

Unable to cast object of type 'System.Windows.Forms.ListViewItem' to type 'System.String'.

Issue answered 29/7, 2013 at 19:46 Comment(0)
K
40

A ListViewItemCollection is exactly what it sounds like - a collection of ListViewItem elements. It's not a collection of strings. Your code fails at execution time for the same reason that this code would fail at compile time:

ListViewItem item = lvFiles.Items[0];
string text = (string) item; // Invalid cast!

If you want a list of strings, each of which is taken from the Text property of a ListViewItem, you can do that easily:

List<string> list = lvFiles.Items.Cast<ListViewItem>()
                                 .Select(item => item.Text)
                                 .ToList();
Kit answered 29/7, 2013 at 19:49 Comment(8)
Why i can only see the files name instead of the whole path ?Issue
@user2214609: I have no idea, given that we didn't even know that the ListView contained filenames. It sounds like you should ask that as a separate question - the contents of the Text property on a ListViewItem which is populated with a filename is a very different matter from converting a collection of ListViewItem values to a List<string>.Kit
What can i do if i don't have Cast or any LINQ functions at all ? (WinForm project)Damoiselle
@user1803300: Why would you not have LINQ?Kit
@JonSkeet I don't have these LINQ ext methods in my WinForm project, it's only available in WPF, what can i do?Damoiselle
@user1803300: Um, no, LINQ has nothing to do with WPF. So long as you're targeting .NET 3.5 or higher, it should be fine. Make sure you have a using directive for System.Linq.Kit
Cast no longer exists.Schnur
@Switch: It really, really does. I suspect you just don't have a using directive for System.Linq, as per the comment from November 2015.Kit
S
5

The Cast method will essentially try to perform a box/unbox, so it will fail if the items in the list aren't already strings. Try this instead:

List<string> list = lvFiles.Items.Cast<ListViewItem>()
                                 .Select(x => x.ToString()).ToList();

Or this

List<string> list = lvFiles.Items.Cast<ListViewItem>()
                                 .Select(x => x.Text).ToList();
Slantwise answered 29/7, 2013 at 19:48 Comment(5)
Won't your first example give you a list where every element is the string "System.Windows.Forms.ListViewItem" ?Tesch
Looks like it will give "ListViewItem: {text here}"Tetrafluoroethylene
@AdamV I'm not sure off the top of my head what ListViewItem.ToString will return (which is why I gave the alternate solution). ListViewItem does override ToString so I assume it does something a little more than just return it's own type name.Slantwise
Why i can only see the files name instead of the whole path ?Issue
Cast no longer exists.Schnur
G
5

Try something like this

List<string> list = lvFiles.Items.Cast<ListViewItem>().Select(x=> x.ToString()).ToList();
Gittel answered 29/7, 2013 at 19:48 Comment(1)
Cast no longer exists.Schnur
G
2

Try this using the Select method:

for list text:

List<string> listText = lvFiles.Items.Select(item => item.Text).ToList();

for list values:

List<string> listValues = lvFiles.Items.Select(item => item.Value).ToList();

Or maybe, for both:

Dictionary<string, string> files = lvFiles.Items.ToDictionary(key => key.Value, item => item.Text);
Gaultheria answered 29/7, 2013 at 19:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.