How to reset bindingsource filter to nothing

前端 未结 4 444
半阙折子戏
半阙折子戏 2021-01-24 12:55

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

相关标签:
4条回答
  • 2021-01-24 13:19

    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:

    productBindingSource.Filter = null;
    

    its possible though that you have to do something to update your UI but usually the bindingSource takes care of that itself.

    0 讨论(0)
  • 2021-01-24 13:25

    Try it like this:

    if (textBox1.Text.Length == 0) {
      productBindingSource.RemoveFilter();
    } else {
      productBindingSource.Filter = "ProductName = '" + textBox1.Text +"'";
    }
    
    // productDataGridView.DataSource = productBindingSource;
    

    The DataGridView shouldn't need to be DataSourced again if it's already using productBindingSource.

    0 讨论(0)
  • 2021-01-24 13:26

    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:

        private void button1_Click(object sender, EventArgs e)
        {
            var qry = (from p in dc.Products
                       select p).ToList();
            BindingList<Product> list = new BindingList<Product>(qry);
            IEnumerable<Product> selection = list.Where(m => m.ProductName.Contains(textBox1.Text) == true);
            productBindingSource.DataSource = selection;
        }
    
    0 讨论(0)
  • 2021-01-24 13:29

    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.

    0 讨论(0)
提交回复
热议问题