如何使用文本框在数据网格视图中搜索数据? [英] How to use textbox to search data in data grid view?

查看:16
本文介绍了如何使用文本框在数据网格视图中搜索数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我当前的代码:

private void searchTextBox_TextChanged(object sender, EventArgs e){(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("Name='{0}'", searchTextBox.Text);}

但是,每当我在文本框中键入内容时,我的数据网格表都会过滤所有内容并变为空白.知道为什么吗?提前谢谢你!

解决方案

您看到空白 DataGridView可能原因是您的过滤器字符串搜索完全匹配到 TextBox 文本.

<块引用>

"名称='{0}'"

因为您是在 TextBox.TextChanged 事件中更新此过滤器,所以第一次输入字符时 - 找不到匹配项.例如,给定以下网格:

╔====╦======╗ ╔========╗║ ID ║ 姓名 ║ searchTextBox ║ ║╠====╬======╣ ╚========╝║ 1 ║ 福║║ 2 ║ 棒 ║║ 3 ║ 巴兹 ║╚====╩======╝

输入Bar会得到如下结果:

╔====╦======╗ ╔========╗║ ID ║ 名称 ║ searchTextBox ║ B ║╠====╬======╣ ╚========╝╚====╩======╝╔====╦======╗ ╔========╗║ ID ║ 名称 ║ searchTextBox ║ Ba ║╠====╬======╣ ╚========╝╚====╩======╝╔====╦======╗ ╔========╗║ ID ║ 名称 ║ searchTextBox ║ Bar ║╠====╬======╣ ╚========╝║ 2 ║ 棒 ║╚====╩======╝

如果是这种情况,我在下面提供了几个选项.如果情况并非如此,那么您就有一个谜团.

<小时>

  1. 完全匹配:考虑使用改为使用以下事件处理程序,以便过滤器仅输入完整搜索文本后应用:

    private void searchTextBox_Leave(object sender, EventArgs e){如果 (string.IsNullOrEmpty(searchTextBox.Text)){(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Empty;}别的{(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("Name='{0}'", searchTextBox.Text);}}

  2. StartsWith 匹配:对文本更改进行更流畅的过滤:

    private void searchTextBox_TextChanged(object sender, EventArgs e){(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("Name LIKE '{0}%'", searchTextBox.Text);}

  3. 包含匹配项:再次,流体过滤:

    private void searchTextBox_TextChanged(object sender, EventArgs e){(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("Name LIKE '%{0}%'", searchTextBox.Text);}

Here's my current code:

private void searchTextBox_TextChanged(object sender, EventArgs e)
    {
        (dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("Name='{0}'", searchTextBox.Text);

    }

However my data grid table filters everything and becomes blank whenever I type something into the textbox. Any idea why? Thank you in advance!

解决方案

The likely reason you are seeing a blank DataGridView is due to your filter string searching for exact matches to the TextBox text.

"Name='{0}'"

Because you are updating this filter in the TextBox.TextChanged event, the first time you enter a character - no matches are found. For example, given the following grid:

╔════╦══════╗                    ╔════════╗
║ ID ║ Name ║      searchTextBox ║        ║
╠════╬══════╣                    ╚════════╝
║ 1  ║ Foo  ║
║ 2  ║ Bar  ║
║ 3  ║ Baz  ║
╚════╩══════╝

Entering Bar will give the following results:

╔════╦══════╗                    ╔════════╗
║ ID ║ Name ║      searchTextBox ║ B      ║
╠════╬══════╣                    ╚════════╝
╚════╩══════╝
╔════╦══════╗                    ╔════════╗
║ ID ║ Name ║      searchTextBox ║ Ba     ║
╠════╬══════╣                    ╚════════╝
╚════╩══════╝
╔════╦══════╗                    ╔════════╗
║ ID ║ Name ║      searchTextBox ║ Bar    ║
╠════╬══════╣                    ╚════════╝
║ 2  ║ Bar  ║
╚════╩══════╝

If this is the case, I've provided a few options below. If this is not the case, then you have a mystery.


  1. Exact matches: consider using the following event handler instead so that the filter is only applied once you've entered the full search text:

    private void searchTextBox_Leave(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(searchTextBox.Text))
        {
            (dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Empty;
        }
        else
        {
            (dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("Name='{0}'", searchTextBox.Text);
        }
    }
    

  2. StartsWith matches: for more fluid filtering on text changes:

    private void searchTextBox_TextChanged(object sender, EventArgs e)
    {
        (dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("Name LIKE '{0}%'", searchTextBox.Text);
    }
    

  3. Contains matches: again, fluid filtering:

    private void searchTextBox_TextChanged(object sender, EventArgs e)
    {
        (dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("Name LIKE '%{0}%'", searchTextBox.Text);
    }
    

这篇关于如何使用文本框在数据网格视图中搜索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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