DataGridView Filter

I searched the Internet for a solution to my problem, I and my studygrp are doing a datagridview that takes information from the classLibrary library. It all works, but now we have to filter it, but all I can find is a bindingsource, but that’s not what I was thinking. I just want a simple filter so you can enter something into the text box and it shows it on a datagridview if it contains this information. I tried:

((DataTable)dataGridView1.DataSource).DefaultView.RowFilter = "FromColumn like '%" + textBox1.Text + "%'"; 

But it doesn't work, as if I want it to help ... anyone who can help?

+4
source share
3 answers

Try BindingSource. It provides good filtering capabilities.

 BindingSource bs = new BindingSource(); bs.DataSource = dataGridView1.DataSource; bs.Filter = "yourColumnName like '%" + textBox1.Text + "%'"; dataGridView1.DataSource = bs; 
+3
source

Try the following:

 DataView dv = ((DataTable)dataGridView1.DataSource).DefaultView; dv.RowFilter = "FromColumn like '%" + textBox1.Text + "%'"; dataGridView1.DataSource = dv; 
+2
source

I found this to best meet my needs:

the data you want to show in the grid is stored in a BindingList (supports INotifyPropertyCchanged, which is pretty neat when updating the list ....) so somewhere you define this BindingList:

 public MyDataList = new BindingList<MyDataItem>(); 

assign it to the grid data source:

  myDataGrid.DataSource = MyDataList; 

when the "Filter" button is clicked, or what event you want to do:

 myDataGrid.DataSource = MyDataList.Where(ln => ln.propertyname.Contains(textBoxFilter.Text).ToList(); 

when resetting the filter, the newly installed source

 MyDataGrid.DataSource = MyDataList 

hope this helps, greetings

+2
source

Source: https://habr.com/ru/post/1444628/


All Articles