DenisJNewb DenisJNewb - 2 months ago 25
C# Question

WPF DataGrid Filter does not work

How can I filter my DataGrid?
This code does not work:

public MainWindow()
{
InitializeComponent();

SetSource();
}

private void FilterTextBox_TextChanged(object sender, TextChangedEventArgs e)
{

}

void SetSource()
{
CollectionViewSource Sorted = new CollectionViewSource() { Source = MainDB.CurDB.Requests };
Sorted.SortDescriptions.Add(new SortDescription("Id", ListSortDirection.Descending));
Sorted.Filter += Sorted_Filter;
MainDataGrid.ItemsSource = Sorted.View;
}

private void Sorted_Filter(object sender, FilterEventArgs e)
{
e.Accepted = true;
}


The Exception I get:




"System.Windows.Data.BindingListCollectionView" It does not support filtering


Update:
The filter is now working, but now I get the following error, that breaks the columns:


System.Windows.Data Error: 40 : BindingExpression path error:
'Changing Date' property not found on 'object' ''RequestsRow'
(HashCode=56483442)'. BindingExpression:Path=Changing Date;
DataItem='RequestsRow' (HashCode=56483442); target element is
'TextBlock' (Name=''); target property is 'Text' (type 'String')


The problem occurs when some cells remain empty

My DataSet. No other classes i have:


Answer

Try this:

List<DataRow> requests = MainDB.CurDB.Requests.Select().ToList();
CollectionViewSource Sorted = new CollectionViewSource() { Source = requests };