Articles in this section
Category / Section

How to Filter a record in the GridTreeControl?

2 mins read

The GridTreeControl does not provide direct support to filter a record. To filter a record, you can use the Filter predicate in the ICollectionView. By using the ICollectionView type as itemssource to the GridTreeControl, you can filter the records in the ICollectionView. So, the records that satisfy the given condition will be displayed in the GridTreeControl.

You can refer to the following code example to use the ICollectionView.

C#

//ICollectionView - used to filter the records in GridTreeControl.     
public ICollectionView CollectionView
{
    get { return collectionView; }
    set
    {
        collectionView = value;
        RaisePropertyChanged("CollectionView");
    }
}
CollectionView = CollectionViewSource.GetDefaultView(this.PersonDetails);       

 

Here, the GetDefaultView method returns the default view of the given source and the PersonDetails is the source (that is the observable collection and having the person details displayed in the Grid).

You can refer to the following code example to define the GridTreeControl in XAML.

XAML

<syncfusion:GridTreeControl Name="treeGrid"
                            Grid.Column="0"
                            ItemsSource="{Binding CollectionView}">
    <syncfusion:GridTreeControl.Columns>
        <syncfusion:GridTreeColumn Width="150"
                                    HeaderText="Last Name"
                                    MappingName="LastName" />
        <syncfusion:GridTreeColumn Width="150"
                                    HeaderText="First Name"
                                    MappingName="FirstName" />
        <syncfusion:GridTreeColumn Width="150"
                                    HeaderText="Date of Birth"
                                    MappingName="DOB" />
        <syncfusion:GridTreeColumn Width="150"
                                    HeaderText="MyEye Color"
                                    MappingName="MyEyeColor" />
    </syncfusion:GridTreeControl.Columns>
</syncfusion:GridTreeControl>

 

Records in the GridTreeControl can be filtered with the help of the Filter Predicate in the ICollectionView. Refer to the following code example to filter the record by using the Filter Predicate.

C#

CollectionView.Filter += Filter;
public bool Filter(object item)
{
    PersonInfo info = item as PersonInfo;
    // When the FilterString matches the MyEyeColor, then the matched records are displayed in the Grid.
    if (info.MyEyeColor == FilterString || string.IsNullOrEmpty(FilterString))
    {
        return true;
    }
    else
    {
        return false;
    }
} 

 

In the following screenshot, the default view of the Grid is displayed.

Figure 1: Default view of grid

The following screenshot displays the entered text (“Blue”) in the textbox is filtered in the Grid.

Figure 2: Entered text filtered in a grid

You can filter the Grid by entering the text in the search textbox. On clicking the ApplyFilter button, the FilterValueMethod command in the viewmodel is invoked. In this command, the CollectionView is refreshed by using the Refresh method and while refreshing the CollectionView, the Filter Predicate is fired and the records are filtered based on the FilterString that holds the text in the search textbox. Finally, the GridTreeControl is redrawn by using the ResetDisplay method.

You can refer to the following code example to call the Refresh method in the CollectionView and ResetDisplay method in the GridTreeControl.

C#

public void FilterValueMethod(object parameter)
{
    var treegrid = parameter as GridTreeControl;
    CollectionView.Refresh();
    treegrid.InternalGrid.ResetDisplay();
}

 

You can refer to the following sample links to filter a records in GridTreeControl for WPF platforms.

Sample Link: FilteringRecordsinGTC_WPF

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied