As titled.
So I was successfully to put DataTable into my CollectionView with using:
ICollectionView _collectionView { get; set; }
public ICollectionView collectionView {......}
DataTable myDataTable= new DataTable();
myConnection.Open();
dbAdpater.Fill(myDataTable);
myConnection.Close();
var collectionList= (newLeadTable as IListSource).GetList();
this.collectionView = CollectionViewSource.GetDefaultView(collectionList);
I know each of the object in the list is DataRowView and as I tested with iterating the data and I can see is stored properly.
The DataGrid was display properly when I execute the code.
But once I added the Filter:
this.collectionView.Filter = new Predicate<object>(
(obj) =>
{
return false; //Just for testing propose
}
);
When I execute the code is giving me error:
Cannot create instance of ‘Window1’ defined in assembly ‘TestWPF,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’. Exception has
been thrown by the target of an invocation. Error in markup file
‘TestWPF;component/Window1.xaml’ Line 1 Position 9.
And output:
A first chance exception of type ‘System.NotSupportedException’
occurred in PresentationFramework.dll
I also tried to stores converts the DataTable into a list of custom object for filtering, but is working fine.
So I wonder what did I done wrong when filtering with DataTable?
See http://msdn.microsoft.com/en-us/library/ms752347.aspx#binding_to_collections
The
BindingListCollectionViewdoes not support filtering. Instead, you have to use the CustomFilter property which takes a filter string and uses it as the value for the underlyingDataView.RowFilterproperty. The string value is SQL that you would use to construct the WHERE clause in a SELECT query.If you want to use multiple conditions, then you have to string them together with AND or OR (just like SQL).