Click here to Skip to main content
15,867,780 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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;
}


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:

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


Add to list view method:

C#
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
Posted
Updated 1-Dec-17 9:01am
Comments
Richard MacCutchan 1-Dec-17 4:59am    
You should filter the data that you are reading from the database, so you start with the correct list of people.
fellanmorgh 1-Dec-17 5:52am    
I am, _people is used to filter it. And if, for example Name is found it will add that specific person to a new list that is then suppose to display all the filter people based on text box input.
Richard MacCutchan 1-Dec-17 6:11am    
Why are you making this so complicated? Use the items from your text boxes to modify the query you send to the database. You will then get an automatically filtered list which you can display immediately to the user.
Kenneth Haugland 1-Dec-17 6:55am    
WPF WinForms, or what?
fellanmorgh 1-Dec-17 9:17am    
WinForms

1 solution

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;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900