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

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

问题描述

下面是我目前的code:

 私人无效searchTextBox_TextChanged(对象发件人,EventArgs的发送)
    {
        (dataGridView1.DataSource如数据表).DefaultView.RowFilter =的String.Format(NAME ='{0}',searchTextBox.Text);    }

不过我的数据网格表过滤一切,每当我输入一些东西到文本框变为空白。知道为什么吗?谢谢你在前进!


解决方案

可能的你看到一个空白的 DataGridView的原因是由于您的过滤字符串搜索到文本框文本完全匹配。


 NAME ='{0}'


由于要更新的​​ TextBox.TextChanged 事件此过滤器,当您第一次输入一个字符 - 没有找到匹配。例如,假设下面的网格:

 ╔════╦══════╗╔════════╗
║║ID名称║searchTextBox║║
╠════╬══════╣╚════════╝
║║1║富
║║2║酒吧
║║3║巴兹
╚════╩══════╝

输入酒吧将给出以下结果:

 ╔════╦══════╗╔════════╗
║║ID名称║║searchTextBox║乙
╠════╬══════╣╚════════╝
╚════╩══════╝
╔════╦══════╗╔════════╗
║║ID名称║║searchTextBox巴║
╠════╬══════╣╚════════╝
╚════╩══════╝
╔════╦══════╗╔════════╗
║║ID名称║║searchTextBox║酒吧
╠════╬══════╣╚════════╝
║║2║酒吧
╚════╩══════╝

如果是这样的话,我下面提供几个选项。如果不是这种情况,那么你有一个谜。



  1. 精确匹配:考虑使用
    下面的事件处理程序,而不是使过滤器仅
    应用一旦你输入完整的搜索文本:

     私人无效searchTextBox_Leave(对象发件人,EventArgs的发送)
    {
        如果(string.IsNullOrEmpty(searchTextBox.Text))
        {
            (dataGridView1.DataSource如数据表).DefaultView.RowFilter =的String.Empty;
        }
        其他
        {
            (dataGridView1.DataSource如数据表).DefaultView.RowFilter =的String.Format(NAME ='{0}',searchTextBox.Text);
        }
    }


  2. StartsWith匹配:作为文本的变化更流畅过滤:

     私人无效searchTextBox_TextChanged(对象发件人,EventArgs的发送)
    {
        (dataGridView1.DataSource如数据表).DefaultView.RowFilter =的String.Format(名称,如{0}%',searchTextBox.Text);
    }


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

     私人无效searchTextBox_TextChanged(对象发件人,EventArgs的发送)
    {
        (dataGridView1.DataSource如数据表).DefaultView.RowFilter =的String.Format(名称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天全站免登陆