C# 使用循环获取 ListView 中的每个项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1343874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:23:52  来源:igfitidea点击:

Using loops to get at each item in a ListView?

c#winformslistviewloops

提问by IbrarMumtaz

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

使用循环获取多列的 ListView 中的每个项目的好而有效的方法是什么?

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.

这将返回一个基于线性数组的表示,表示属于 ArrayList 集合对象中的目标 ListView 控件的所有项目。

采纳答案by mrtaikandi

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

我建议使用 IEnumerable 作为方法的返回类型,并使用“yield return”来返回每个子项。

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.

尽管如果您使用 .NET 3.5,我也建议使用 LINQ。

回答by Mayo

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.

试一试,让我们知道它是否有效......不过我同意其他评论。我认为将其转换为数组列表的唯一原因是,如果您要将它保存在 Web 表单以外的其他地方。当然不是用于处理列表视图。

EDIT #2:

编辑#2:

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

loggerlistView.Items 的类型为“System.Collections.Generic.IList”,并且有一个称为“ToList”的方法,如here所述。

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

尝试添加对 Linq 的引用?直到现在我才意识到这是特定于 .NET 3.5 的 - 抱歉造成混乱。

using System.Linq;

回答by IbrarMumtaz

    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.

这就是我的做法。

回答by Tim Jarvis

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

如果您有 .NET 3.5,另一种 IMO 更简单的方法来执行您上面显示的操作

  var qry = from i in listView1.Items.Cast<ListViewItem>()
            from si in i.SubItems.Cast<System.Windows.Forms.ListViewItem.ListViewSubItem>()
            select si.Text;