Denis Morgan

Denis Morgan

  • 2.1k
  • 72
  • 10.2k

Filter Datagrid View from data populated from entity in C#

Aug 4 2020 6:59 AM
I am newbie in C# and probably this might be a possible duplicate but am unable to do filter of records on datagrid view whose columns were designed from designer view on TextBox change event.
 
This is my code to populate the datagridview. Any help will be highly appreciated.
  1. IList<ProductEntity> emp = HibernateDao.fetchDAta();  
  2. IList<ProductModel> products = new List<ProductModel>();  
  3. foreach(ProductEntity e in emp)  
  4. {  
  5.     dataGridView1.Rows.Add("" + e.id, e.barcode, e.product_name, e.product_desc, e.quantity + " " + e.units,""+e.buying_price,e.retail_selling_price,e.can_sell_whole_sale,e.whole_selling_price);  
  6. }  
And this is the code that have tried to do filter which is not working at all
  1. BindingSource bs=new BindingSource();  
  2. private void metroTextBox1_TextChanged(object sender, EventArgs e)  
  3. {  
  4.     if (metroTextBox1.Text == string.Empty)  
  5.     {  
  6.         bs.RemoveFilter();  
  7.     }  
  8.     else  
  9.     {  
  10.         bs.Filter = string.Format("product_name LIKE '*{0}*'", metroTextBox1.Text);  
  11.     }  
  12. }  

Answers (1)