使用TextBox过滤Datagridview行 [英] Filter Datagridview rows using TextBox

查看:186
本文介绍了使用TextBox过滤Datagridview行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用下面的代码:



$($ Data $)
$($(DataTable)dataGridView3.DataSource)
$ $ { .DefaultView.RowFilter = string.Format(LibService like'%{0}%',ChercheStextBox.Text.Trim()。Replace(',''));
}
catch
{

}

}

但是这段代码并没有过滤datagridView,即使我在另一个datagridView中使用了相同的代码



。我不知道我的代码中的

错误在哪里?



在此先感谢。

>

编辑:

删除了try catch,我得到了这个错误信息:


无法将类型为system.windows.forms.bindingsource的对象转换为键入system.data.datatable

我该如何解决这个问题?

解决方案

DataSource是一种BindingSource而不是DataTable,所以试试这个代码:

pre $ private私人无效ChercheStextBox_TextChanged(对象发件人,EventArgs e)
{
var bd =(BindingSource)dataGridView3.DataSource;
var dt =(DataTable)bd.DataSource;
dt.DefaultView.RowFilter = string.Format(LibService like'%{0}%',ChercheStextBox.Text.Trim()。Replace(',''));
dataGridView3.Refresh();


}


I have a binded datagridView and i want to filter it using a TextBox value.

I used this code:

private void ChercheStextBox_TextChanged(object sender, EventArgs e)
    {
        try
        {
            ((DataTable)dataGridView3.DataSource).DefaultView.RowFilter = string.Format("LibService like '%{0}%'", ChercheStextBox.Text.Trim().Replace("'", "''"));
        }
        catch
        {

        }

    }

But this code doesn't filter the datagridView even i have used the same code

in another datagridView and it works perfectly. I don't know where is the

error in my code?

Thanks in advance.

EDIT:

i removed try catch and i got this error message:

unable to cast object of type 'system.windows.forms.bindingsource' to type 'system.data.datatable'

How can i fix it??

解决方案

The DataSource is a type of BindingSource not DataTable, so try this code:

private void ChercheStextBox_TextChanged(object sender, EventArgs e)
    {
        var bd = (BindingSource)dataGridView3.DataSource;
        var dt = (DataTable)bd.DataSource;
        dt.DefaultView.RowFilter = string.Format("LibService like '%{0}%'", ChercheStextBox.Text.Trim().Replace("'", "''"));    
        dataGridView3.Refresh();


    }

这篇关于使用TextBox过滤Datagridview行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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