Why ICollectionView Filter is working?

I understand the grid does not support ICollectionView Filtering from other forum posts. I really would like to do the filtering the MVVM way without leaking UI and ViewModel. I was trying various methods and the following method actuaully do the filtering correctly!

ObservableCollection AllCompanies = new ObservableCollection();
  public ICollectionView CompanyView { get; set; }

public CreateObjects()
{
  AllCompanies.Add(new FinanceCompanyDetails("Finance_MorganStanley"));
            AllCompanies.Add(new FinanceCompanyDetails("Finance_special"));           
            AllCompanies.Add(new MarketingCompanyDetails("Marketing_def"));
            AllCompanies.Add(new MarketingCompanyDetails("Marketing_special"));

   ICollectionView comnpanyView2 = vs2.View;
    comnpanyView.SortDescriptions.Add(new SortDescription("Order", ListSortDirection.Ascending));
          
    comnpanyView.Filter = (x) => ((CompanyDetails)x).CompanyName.StartsWith("Marketing");
}

xaml

  
                             
                              syncfusion:SfDataGrid 
                           HorizontalAlignment="Left"
                            x:Name="AllBuyOrdersGrid"
                            DataContext="{Binding viewModel2}"
                            ItemsSource="{Binding Path=CompanyView}"
                            ShowGroupDropArea="False"
                            AllowSorting="False"
                            AutoGenerateColumns="True"
                            HeaderRowHeight="10"
                            CanUseViewFilter="True">


If the following line is commented, the code will throw an exception. But otherwise the filtering is working correctly.

What is the reason for this behavior? Can I rely on the fact that based on that code I can set the Filter in the viewmodel using an ICollectionView? I want to make sure I am not relying on some undocumented behavior which could potentially change in subsequent updates 


1 Reply

SJ Sathiyathanam Jeyakumar Syncfusion Team August 2, 2018 11:58 AM UTC

Hi Jimmy,  
Thank you for contacting Syncfusion support.  
 
We have analyzed your query and we don’t have direct support to apply the filters in view model CollectionView.  And you can achieve your requirement by customizing the GridQueryableCollectionViewWrapper and override the FilterRecord method to apply the filtering to the SfDataGrid like below, 
Code Snippet: 
 
//itemssource for grid as CustomizedQueryablecollectionviewrapper 
private CustomQueryableCollectionViewWrapper collectionView; 
public CustomQueryableCollectionViewWrapper CollectionView 
{ 
get { return collectionView; } 
set 
{ 
collectionView = value; 
RaisePropertyChanged("CollectionView"); 
} 
} 
 
//CollectionView is the type of customized GridQueryablecollectionViewWrapper which is the itemssource of sfdatagrid. 
(this.datagrid.DataContext as ViewModel).CollectionView = newCustomQueryableCollectionViewWrapper(emp as IEnumerable,ViewModel.SfGridValue); 

Please refer the below code snippet to define the SfDataGrid in xaml.
Code Snippet: 
 
<Syncfusion:SfDataGrid x:Name="datagrid" 
AllowFiltering="True" 
AutoGenerateColumns="False" 
ColumnSizer="SizeToHeader" 
FrozenRowsCount="4" 
ItemsSource="{Binding CollectionView}"  
local:ViewModel.DataGrid="{Binding ElementName=datagrid}"> 

Please refer the following code snippet to override GridQueryableCollectionViewWrapper.
Code Snippet: 
//customized GridQueryablecollectionViewWrapper 
public class CustomQueryableCollectionViewWrapper :GridQueryableCollectionViewWrapper 
{ 
public CustomQueryableCollectionViewWrapper(IEnumerable source, SfDataGridgrid) 
base(source, grid) 
{ 
 
} 
 
public override bool FilterRecord(object record) 
{ 
var item = record as BusinessObjects; 
            if (item.EmployeeID >10) 
                return true; 
            return false; 
} 

We have also prepared the sample based on this and please find the sample under the following location, 
 
In the above sample, we have filtered the data if the Employee ID is greater than 10. 

Could you please refer the above sample and let us know if you need further assistance on this. 
 
Regards, 
Sathiyathanam 


Loader.
Up arrow icon