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.
Unfortunately, activation email could not send to your email. Please try again.

Filter

Thread ID:

Created:

Updated:

Platform:

Replies:

119487 Jun 30,2015 06:27 AM Oct 5,2015 10:25 PM WPF 7
loading
Tags: SfDataGrid
Soumyajit Roy
Asked On June 30, 2015 06:27 AM

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
 

Elavarasan M [Syncfusion]
Replied On July 1, 2015 11:39 PM

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


Soumyajit Roy
Replied On August 3, 2015 01:08 PM

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.

Jai Ganesh S [Syncfusion]
Replied On August 4, 2015 12:32 PM

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

Soumyajit Roy
Replied On August 10, 2015 07:35 AM

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

Jai Ganesh S [Syncfusion]
Replied On August 10, 2015 11:56 PM

Hi Soumyajit,


Thank you for the update.


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


Thank you,

Jai Ganesh S


DataGrid
Replied On October 1, 2015 04:37 AM

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!

Jai Ganesh S [Syncfusion]
Replied On October 5, 2015 10:25 PM

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


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;