简体   繁体   中英

Get specific listview rows based on condition

I have a listview with diffrent entries (see figure (A). I would like to extract some specific rows based on a condition. So far, i have this code:

 private void Button2_Click(object sender, EventArgs e)
 {
    ArrayList listing = new ArrayList();

    for (int i = 0; i < listView2.Items.Count; i++)
    {
        string columnOne = listView2.Items[i].Text;
        string columnTwo = listView2.Items[i].SubItems[1].Text;
        int numb = int.Parse(listView2.Items[i].SubItems[2].Text);
        string columnThree = listView2.Items[i].SubItems[3].Text;

        if(numb >= 2)
        {
            listing.Add($"{columnOne},{columnTwo},{numb},{columnThree}");
        }
    }
    foreach (string item in listing)
    {
        listView2.Items.Clear();
        ListViewItem listItem = new ListViewItem();
        var separ = item.Split(',');
        listItem.Text = separ[0].Trim();
        listItem.SubItems.Add(separ[1]);
        listItem.SubItems.Add(separ[2]);
        listItem.SubItems.Add(separ[3]);

        listView2.Items.Add(listItem);
    }
}

在此处输入图像描述

I get figure (B), but normally i should get figure (C). How can this be achieved?

you shouldn't clear listview in foreach loop. do it once:

listView2.Items.Clear();
foreach (string item in listing)
{
    // listView2.Items.Clear();
    ListViewItem listItem = new ListViewItem();
    var separ = item.Split(',');
    listItem.Text = separ[0].Trim();
    listItem.SubItems.Add(separ[1]);
    listItem.SubItems.Add(separ[2]);
    listItem.SubItems.Add(separ[3]);

    listView2.Items.Add(listItem);
}

Removing the non matching items from the list makes more sense here. For your problem, execute a backward loop, try to convert the text of the third subitem to integer value using the int.TryParse method, and remove the ListViewItem if the value is less than 2.

private void button2_Click(object sender, EventArgs e)
{
    for (var i = listView2.Items.Count - 1; i >= 0; i--)
    {
        if (int.TryParse(listView2.Items[i].SubItems[2].Text, out var num) && num < 2)
        {
            listView2.Items.RemoveAt(i);
        }
    }
}

Yet, if you want to get a list of matching items:

// +
using System.Collections.Generic;

private void button2_Click(object sender, EventArgs e)
{
    var items = new List<ListViewItem>();

    for (var i = 0; i < listView2.Items.Count; i++)
    {
        if (int.TryParse(listView2.Items[i].SubItems[2].Text, out var num) && num >= 2)
        {
            items.Add(listView2.Items[i]);
        }
    }
    // ...
}

Or LINQ way:

// +
using System.Linq;

private void button2_Click(object sender, EventArgs e)
{
    var items = listView2.Items.Cast<ListViewItem>()
        .Where(x => int.TryParse(x.SubItems[2].Text, out var num) && num >= 2)
        .ToList();

    // ...
}

As a side note, using the ArrayList class is not recommended , use the List<T> class instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM