如何在列表视图中过滤C# [英] How do I filter in a list view C#

查看:68
本文介绍了如何在列表视图中过滤C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在做一个家庭项目。基本上我有一个用于从数据库加载数据的列表。这工作正常,数据被加载到列表,从该列表我可以填充列表视图控件。



现在我正在尝试创建一个过滤器,它只是根据文本框中的用户输入过滤列表视图。所以我有一个列表视图:



名字姓氏地址

Tom Nah 1

Kerrt Nah 2

罗伯特戴尔3



如何做到这一点,无论用户输入文本框并单击按钮事件,它都会过滤整个列表并且基于用户输入仅返回他们正在搜索的项目。例如,如果他们搜索Surname'Nah',它将显示Tom和Kerry的整行,因为它们都包含姓'Nah'



注意:有每个3个文本框用于特定行。所以txtName,txtSurname和txtAddress。



我尝试过:



我试图通过以下方案过滤:



I am currently working on a home project. Basically I have a list that is being used to load data from the database. That works fine, data is being loaded to a list and from that list I am able to populate the list view control.

Now I am trying to create a filter that will simply filter the list view based on user input in text box. So I have a list view:

Name Surname Address
Tom Nah 1
Kerrt Nah 2
Robert Dell 3

How to do it so that whatever user enters in a textbox and click a button event, It will filter through the whole list and based on the user input return only the items that they were searching for. For example, if they search for Surname 'Nah', it will display the whole row of Tom and Kerry because they both contain surname 'Nah'

NOTE: There are 3 text boxes each of them is used for specific row. So txtName, txtSurname and txtAddress.

What I have tried:

I have tried to Filter through schemes like that:

private List FilterPeople()
{
    List filteredPeople = new List();

    string Name= txtName.Text.ToLower();
    string Surname= txtSurname.Text.ToLower();
    string Address = txtAddress.Text.ToLower();

    listView.Items.Clear();

    foreach (var person in _people) //_people is the list that is getting the
                                    //data from the database.
    {
        if (!string.IsNullOrEmpty(Name) || !string.IsNullOrEmpty(Surname) ||
            !string.IsNullOrEmpty(Address))
        {
            if ((!person.Name.ToLower().Contains(Name)) ||
                (!person.Surname.ToLower().Contains(Surname)) ||
                (!person.Address .ToLower().Contains(Address)))
            {
                continue;
            }
        }


        filteredPeople.Add(person);
    }

    return filteredPeople;
}





问题是filterPeople总是为0.每当我确实点击'持续'时在它上面放了一个断点,似乎它找到了姓'Nah'。但由于某种原因,我不知道它在filteredPeople中返回0是什么。



这是我的点击事件按钮:





The problem is that filteredPeople is always 0. It did actually hit 'continiue' whenever I put a breakpoint on it and it seems that it found surname 'Nah'. But for some reason I don't know what it is returning 0 in filteredPeople.

This is my click event button:

private void btnApplyFilters_Click(object sender, EventArgs e)
{
    foreach (var person in FilterPeople())
    {
        AddToListView(person);
    }
}





添加至列表视图方法:





Add to list view method:

private void AddItemToListView(Person person)
{
    ListViewItem lv = new ListViewItem(person.Name);
    lv.SubItems.Add(person.Surname);
    lv.SubItems.Add(person.Address);

    lvLog.Items.Add(lv);
}





任何帮助将不胜感激。

谢谢



Any help would be appreciated.
Thanks

推荐答案

试试这样的事情

Try something like this
List filteredPeople = new List();

string Name = txtName.Text.Trim().ToLower();
string Surname = txtSurname.Text.Trim().ToLower();
string Address = txtAddress.Text.Trim().ToLower();

//it would be a good idea to move this line outside this method,
//since filter logic should not have any affect on the actual view
listView.Items.Clear();

foreach (var person in _people) //_people is the list that is getting the
                                //data from the database.
{
    if(!String.IsNullOrEmpty(Name)
       && person.Name.ToLower().Contains(Name))
    {
        filteredPeople.Add(person);
    }
    else if (!String.IsNullOrEmpty(Surname)
             && person.Surname.ToLower().Contains(Surname))
    {
        filteredPeople.Add(person);
    }
    else if (!String.IsNullOrEmpty(Address)
             && person.Address.ToLower().Contains(Address))
    {
        filteredPeople.Add(person);
    }
}

return filteredPeople;


这篇关于如何在列表视图中过滤C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆