Using BindingSource
on LINQ to SQL, and having implemented a BindingList
in my project, I have to use a Textbox
to filter rows in a DataGridView
, so when I delete the textbox content, Filter should be reset to nothing.
My code is as follows:
if (textBox1.Text.Length == 0)
{
productBindingSource.Filter = null;
}
else
{
productBindingSource.Filter = "ProductName = '" + textBox1.Text +"'";
//productBindingSource.RemoveFilter();
}
productDataGridView.DataSource = productBindingSource;
But this does nothing, any idea, please?
I assume you test if textbox is empty in TextChanged event. Maybe your method is not being called when Text length = 0. I don't remember exactly why but i experienced this case before.
If you are using a BindingList you wrote, provide code. RemoveFilter, setting Filter to null or empty string has always worked for me.
http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.filter.aspx
as shown there the
bindingsource.Filter
is a string value. And default is null, so just do this:its possible though that you have to do something to update your UI but usually the bindingSource takes care of that itself.
Try it like this:
The DataGridView shouldn't need to be DataSourced again if it's already using productBindingSource.
I found that "Find" method cannot be used directly with BindingList, but fortunately there is an alternative, using IEnumerable. After Implementing a BindingList in the project, I can filter a bound datagridview using the next code: