Using loops to get at each item in a ListView?
Asked Answered
C

5

5

What is a nice and effective way of getting at each item in a ListView of more than one column using loops?

After doing a fair bit of digging around I couldn't really find anything so I when I did find something I wanted to share it on here see if people have better ways of doing it. Also sort of like preempting a question that is bound to come up as I was scratching my head for a bit thinking how do ??? eerrrr .... I ?

I like this website so I wanted to share my solution to the question above. Sort of backwards I know but still, I know it will help someone out somwhere. = )

    private ArrayList getItemsFromListViewControl()
    {                      
        ArrayList lviItemsArrayList = new ArrayList();

        foreach (ListViewItem itemRow in this.loggerlistView.Items)
        {
            //lviItemsArrayList.Add(itemRow.Text); <-- Already included in SubItems so ... = )

            for (int i = 0; i < itemRow.SubItems.Count; i++)
            {
                lviItemsArrayList.Add(itemRow.SubItems[i].Text);
                // Do something useful here, for example the line above.
            }
        }
        return lviItemsArrayList;
    }

This returns a linear array based representation of all the items belong to a targeted ListView Control in an ArrayList collection object.

Civilly answered 27/8, 2009 at 21:38 Comment(3)
What advantage does this copying of data into an ArrayList have over just iterating through Items directly?Kabob
You should have asked the question then answered it your self, then marked it as accepted, (of course write the answer of line, and copy and paste it) before anyone gets a chance to answer it.Lamee
Yes you are right. In the example I do iterate through the items directly but I am persisting my ArrayList object somewhere else so I needed an arraylist object that could be of any size depending on the given amount of rows in my ListView. So i needed to process values of each item in the ListView and then feed my results into another part of my application. An arraylist allowed me to achieve. As it allowed the collection to grow dynamically as the loops encountered a new row in the List.Civilly
P
6

I suggest using IEnumerable as the return type of the method and using "yield return" to return each subitems.

private IEnumerable<ListViewSubItem> GetItemsFromListViewControl()
{                      
    foreach (ListViewItem itemRow in this.loggerlistView.Items)
    {            
        for (int i = 0; i < itemRow.SubItems.Count; i++)
        {
            yield return itemRow.SubItems[i]);
        }
    }
}

although if you are using .NET 3.5 I suggest using LINQ too.

Petal answered 28/8, 2009 at 14:0 Comment(1)
This is a nice way of doing and it looks professional as well, I'm a still a little flakey with this one, would you mind explaining how the return type on this one works? TIACivilly
C
4
    foreach (ListViewItem itemRow in this.ListView.Items)
    {            
        for (int i = 0; i < itemRow.SubItems.Count; i++)
        {
            // Do something useful here !
            // e.g 'itemRow.SubItems[count]' <-- Should give you direct access to
            // the item located at co-ordinates(0,0). Once you got it, do something 
            // with it.
        }
    }

Thats my way of doing it.

Civilly answered 27/8, 2009 at 22:14 Comment(1)
Why not just foreach the subitems as well? foreach (ListViewItem.ListViewSubItem subItem in itemRow.SubItems) {...}Mouthy
A
0

If you have .NET 3.5, another and IMO easier way to do what you show above is

  var qry = from i in listView1.Items.Cast<ListViewItem>()
            from si in i.SubItems.Cast<System.Windows.Forms.ListViewItem.ListViewSubItem>()
            select si.Text;
Alvey answered 27/8, 2009 at 22:39 Comment(0)
D
0

I use this for pulling one or more lines from a listview using foreach:

First I connect the listviews to an event:

    private void generic_KeyDown(object sender, KeyEventArgs e)
    {
        ListView gListBox = (ListView)sender;

        if (e.Control && e.KeyCode == Keys.C)
        {
            CopyListViewBox(gListBox);
        }
    }

Then I get the selected data:

    public void CopyListViewBox(ListView list)
    {

        StringBuilder sb = new StringBuilder();
        foreach (ListViewItem item in list.SelectedItems)
        {
            string line = "";
            foreach(ListViewItem.ListViewSubItem element in item.SubItems)
            {
                line += element.Text + "\t";
            }
            sb.AppendLine(line);
        }

        Clipboard.SetDataObject(sb.ToString());

    }
Disinter answered 13/11, 2020 at 19:53 Comment(0)
H
-1

I don't have time to test this with code, but perhaps you could initialize the array list with a collection returned by one of the listview methods? I'll check this when I get home and comment if the question isn't answered before then.

EDIT: I gave this a real quick check and it compiled, ran, and initialized the arraylist with one element that I put in the listview.

ArrayList lviItemsArrayList = new ArrayList(loggerlistView.Items.ToList());

Give it a shot and let us know if it works... I agree with the other comment though. I figure the only reason to convert it to an array list was if you were going to persist it somewhere other than a Web form. Certainly not for processing the list view.

EDIT #2:

loggerlistView.Items is of type "System.Collections.Generic.IList" and that has a method called "ToList" as described here.

Try adding a reference to Linq? I didn't realize this was specific to .NET 3.5 until now - sorry for the confusion.

using System.Linq;
Heeling answered 27/8, 2009 at 21:44 Comment(2)
You've lost me on this one, I cannot see any intellisense listing for .ToList() on ListView.Items --> Therefore won't compile for me. Can you explain further?Civilly
same, the answer is wrong, no .ToList() method is available.Blown

© 2022 - 2024 — McMap. All rights reserved.