【问题标题】:Filtering DataGridView over two columns in one textbox在一个文本框中过滤两列的 DataGridView
【发布时间】:2015-09-23 06:09:04
【问题描述】:

我正在处理一些从 WebService 列表中获取的数据,并将其呈现在 DataGridView 中。 DataGridView 包含 4 列:ID、名称、姓氏和权限。我想在一个 textbox_TextChanged 事件中过滤 DataGridView 元素。

重点是按姓名搜索,然后当它找到全名时,用户输入空格并按与姓名匹配的姓氏进行搜索。

到目前为止我完成的代码:

 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);
        }
    }

问题在于,当用户开始正确输入名称,然后在某些时候输入错误。在这种情况下,DataGridView 将显示最后一次正确匹配。

另一个问题是,当DataGridView中有两个同名的人时,当用户键入它时,它只显示第一个。

任何修改我的代码的帮助将不胜感激。谢谢!

【问题讨论】:

  • 您应该考虑使用DataTable 作为DataSource,然后使用DataTable.DefaultView.RowFilter = "col1 = '...' and col2 = '...'"

标签: c# winforms datagridview textchanged


【解决方案1】:

你可以这样做:

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

【讨论】:

  • 哇,这正是我想要的。谢谢!
猜你喜欢
  • 1970-01-01
  • 2013-06-24
  • 1970-01-01
  • 2018-03-14
  • 2013-01-31
  • 2019-05-25
  • 1970-01-01
  • 2012-07-25
  • 2014-07-01
相关资源
最近更新 更多