使用TextBox过滤Datagridview行 [英] Filter Datagridview rows using TextBox
问题描述
我使用下面的代码:
$($ 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屋!