Search code examples
c#winformsdatagridviewtextchanged

Filtering DataGridView over two columns in one textbox


I'm working on some data I'm getting from list from WebService and presenting it in DataGridView. DataGridView contains 4 columns: ID, Name, Surname and Permissions. I would like to filter DataGridView elements in ONE textbox_TextChanged event.

The point is to search by name and then when it finds whole name the user enters space and is searches by surname that matches the name.

The code I've done so far:

 private void textBoxSearch_TextChanged(object sender, EventArgs e)
    {
        newList.Clear();
        newlist2.Clear();

        search = textBoxSzukaj.Text;
        try
        {
            foreach (localhost.Person item in listPerson.ToList())
            {
                if (item.name.Contains(search))
                {
                    newList.Add(item);
                    dataGridViewOsob.DataSource = search == "" ? listPerson : newList;
                    if (search.Length == item.name.Length)
                    {
                        name = search;
                        abc = name + " ";
                        searchBySurname = true;
                    }
                    else
                    {
                        searchBySurname = false;
                    }
                }

                else
                {
                   //dataGridViewPerson.DataSource = null;
                }
            }
            if (searchBySurname == true)
            {
                if (search.Length > abc.Length)
                {
                    searchBySurname = textBoxSearch.Text;
                    searchBySurname = search.Remove(0, abc.Length);
                    foreach (localhost.Person itemm in listPerson.ToList())
                    {
                        if (itemm.name == name)
                        {
                            if (itemm.surname.Contains(searchNazwisko))
                            {

                                if (searchNazwisko.Length > 0)
                                {
                                    newList.Clear();

                                    newlist2.Add(itemm);
                                    dataGridViewPerson.DataSource = search == "" ? listPerson : newlist2;
                                }
                                else
                                {
                                }
                            }
                            else
                            {
                            }
                        }

                    }
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

The problem is that when the user starts typing the name correctly and then at some point he types it wrongly. At this situation, the DataGridView will show the last correct match.

Another problem is that when there are two persons in DataGridView with the same name, it only shows the first one when user types it.

Any help in modyfying my code would be very appreciated. Thanks!


Solution

  • You can simply do this:

    dataGridViewPerson.DataSource = listPerson.ToList().Where(x => (x.name + " " + x.surname)
                                                       .Contains(textBoxSearch.Text)).ToList();