We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Filter

Hi,

I am trying to filter a SfDataGrid collection in my view model code using the following code and it does not work.

private void ApplyFilterToList()
{
            ICollectionView view = CollectionViewSource.GetDefaultView(list);
            view.Filter += new Predicate<object>(FilterRecords);
            view.Refresh();
}

private bool FilterRecords(object item)
{
................................
}
view is null.

Is there any similar function like the above one which returns the view object associated with the SfDataGrid collection.

Thanks,
Soumyajit Roy
 

8 Replies

EM Elavarasan M Syncfusion Team July 2, 2015 03:39 AM UTC

Hi Soumyajit,


Thank you for using Syncfusion products.


We have analysed your query and currently we don’t have direct support to apply the filters in view model collectionview. You can bind the collectionview to the grid and filter the grid using Filter predicate in sfdatagrid. We have prepared the sample for Binding CollectionView with SfDataGrid and set the ItemSource as CollectionView to Grid.  Then the Sorting, Filtering, Grouping operations are performed through “SfDataGrid.View”.  Please find the sample from following location:

Sample: http://www.syncfusion.com/downloads/support/forum/119487/ze/SfDataGridSample17393417


Please let us know if you have any queries.

Thanks,
Elavarasan M



SR Soumyajit Roy August 3, 2015 05:08 PM UTC

Thank You for the reply.
Sorry for bit late to follow up.

I have bound the ItemSource of sfDataGrid object to the collection's view
ICollection CollectionView1 = CollectionViewSource.GetDefaultView(records1);  // records1 is the collection

Here is the XAML:
<syncfusion:SfDataGrid x:Name="dg" ItemsSource="{Binding Path=CollectionView1}" />

But in the viewmodel, when I add the filter, the filter does not seem to be applied.
CollectionView1.Filter += new Predicate<object>(FilterRecords);
CollectionView1.Refresh();

Any idea?

Thanks.


JG Jai Ganesh S Syncfusion Team August 4, 2015 04:32 PM UTC

Hi Soumyajit,
Thank you for the update.
We have analyzed your query. As we said in our previous update, we don’t have direct support to apply the filters in view model collectionview. However you can achieve your requirement by using custom QueryableCollectionView and overriding FilterRecord method.

GridQueryableCollectionViewWrapper and set it as the itemssource of SfDataGrid. Please find the below code snippet:


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 = new CustomQueryableCollectionViewWrapper(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, SfDataGrid grid)

: 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,
Sample: http://www.syncfusion.com/downloads/support/directtrac/142249/ze/SfDataGridSample-759408692
In the above sample , we have filtered the data if the Employee ID is greater than 10.
Please let us know if you need further assistance.
Thank you,
Jai Ganesh S


SR Soumyajit Roy August 10, 2015 11:35 AM UTC

Hello Jai Ganesh,

Thank you so much your quick response as always. Your solution will work definitely but I had already found an alternate solution. I have passed the CollectionViewAdv from sfDataGrid to my view model and I can add the filter there and it worked.

We can close this thread. 

Kind Regards.
Soumyajit Roy


JG Jai Ganesh S Syncfusion Team August 11, 2015 03:56 AM UTC

Hi Soumyajit,


Thank you for the update.


Please let us know if you need further assistance on this.


Thank you,

Jai Ganesh S



DA DataGrid October 1, 2015 08:37 AM UTC

Hi Soumyajit,

Could you please tell me how you managed to pass the CollectionViewAdv to your model?
Synchfusion's solution is not usable and absolutely not MVVM friendly.

Thanks!


JG Jai Ganesh S Syncfusion Team October 6, 2015 02:25 AM UTC

Hi Soumyajit,


As we already mentioned in our first update, currently we don’t have a direct support to apply the filters in view model collectionview. Hence we have provided the solution to filter a grid using view filter and also provided a solution for overriding   GridQueryableCollectionViewWrapper and apply the filter but in your last update you have  pass the CollectionViewAdv to your model and apply the filter. Could you please share the details for how you pass the CollectionViewAdv to your model or please share the sample for this? This would be more helpful for us.


Please let us know if you have any query.


Regards,

Jai Ganesh S



AD Adam replied to DataGrid June 24, 2020 08:12 PM UTC

Hi Soumyajit,

Could you please tell me how you managed to pass the CollectionViewAdv to your model?
Synchfusion's solution is not usable and absolutely not MVVM friendly.

Thanks!

I know this question is 5 years old, but I ran into this issue, and was able to somewhat resolve it, using a tiny bit of code-behind in the view.  I'm leaving this for anyone in the future who might also need this solution:

In the view code behind:
        private void _dataGrid_ItemsSourceChanged(object sender, GridItemsSourceChangedEventArgs e)
        {
            (this.DataContext as ViewModel).InvoiceFilter = _dataGrid.View;
        }

In the viewmodel:
        public ICollectionViewAdv InvoiceFilter { get; set; }

Apply the filter predicate as normal in the viewmodel.

Loader.
Live Chat Icon For mobile
Up arrow icon