Search code examples
c#wpfgridviewfilterfill

how to filter a wpf gridview that is filled with MySqlDataAdapter?


this is how my data gridview is filled:

using (MySqlDataAdapter a = new MySqlDataAdapter(query, conn))
{

    DataTable dt = new DataTable();
    a.FillAsync(dt);
    dataGridView1.ItemsSource = dt.DefaultView;

}

now i used to filter my data-grid view in winforms this way:

string rowFilter = string.Format("[{0}] LIKE '%{1}%'", "full name", pattern);
(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = rowFilter;

here is the wpf version of it which does not work

string rowFilter = string.Format("[{0}] LIKE '%{1}%'", "full name", pattern);
((DataView)dataGridView1.ItemsSource).ToTable().DefaultView.RowFilter = rowFilter;

the above wpf version code runs ok but it doesn't filter the gridview instead it passes over it (the code ) like nothing happened?

at last How can i filter my wpf datagridview?

P.S: i am both new and not good when it comes to xaml and that's why i prefer c# code solutions.


Solution

  • Thank God ... i just found the solution!!!

    string rowFilter = string.Format("[{0}] LIKE '%{1}%'", "full name", pattern);
    ((DataView)dataGridView1.ItemsSource).RowFilter = rowFilter;
    

    i was using the DefaultView.RowFilter which does not work when instead i should've used the DataView.RowFilter (Works like charm).

    and for anyone new on row filtering here is what i consider row filter cheat sheet.