DataFilters / Custom Filter
Custom Filter

The DataFilter control allows you to create custom filters which can replace the default filter editors at runtime. It provides CustomFilter class which needs to be inherited for defining a custom filter. Inheriting the CustomFilter class provides access to its properties and methods which can further be used to create a custom filter in .NET and .NET Framework editions.

The application depicts customized filtering of card models.

Following example shows how you can create and add custom filter to the DataFilter control. It demonstrates how you can filter the car models using the MultiSelect control as a part of the DataFilter control UI.

  1. Define a custom filter by creating a new class which inherits the CustomFilter class. In the class constructor, use Control property of the CustomFilter class to specify the custom control that you want to use to perform filtering in the DataFilter control.
    Public Sub New()
        _modelFilterPresenter = New ModelFilterPresenter()
        Control = _modelFilterPresenter
        AddHandler _modelFilterPresenter.SelectedChanged, AddressOf OnValueChanged
    End Sub
    
  2. Bind the MultiSelect control with data. In this example, we are binding the control with data using the SetTagList method.
    Public Sub SetTagList(ByVal itemSource As IEnumerable(Of Car))
        _modelFilterPresenter.SetTagList(itemSource)
    End Sub
    
  3. Raise the SelectionChanged event of the MultiSelect control. This notifies the DataFilter control about the change in filter values.
    Private Sub MultiSelect_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs)
        SelectedChangedEvent?.Invoke(Me, e)
    End Sub
    
    Public Sub SetTagList(ByVal itemSource As IEnumerable(Of Car))
        For Each item As C1MultiSelect In Items
            RemoveHandler item.SelectionChanged, AddressOf MultiSelect_SelectionChanged
        Next item
        Items.Clear()
        Dim ms = New C1MultiSelect With {
            .Height = 50,
            .PlaceHolder = "Model of car",
            .ItemsSource = itemSource,
            .DisplayMemberPath = "Model",
            .SelectedValuePath = "Model",
            .AutoCompleteMode = AutoCompleteMode.SuggestAppend
        }
        AddHandler ms.SelectionChanged, AddressOf MultiSelect_SelectionChanged
        Items.Add(ms)
    End Sub
    
  4. Implement the GetExpression abstract method of the CustomFilter class to create and return the filter expression which is to be used to filter the values in the data aware control.
    Protected Overrides Function GetExpression() As Expression
        Dim tags = _modelFilterPresenter.GetSelectedTagList()
        Dim expr = New CombinationExpression() With {.FilterCombination = FilterCombination.Or}
        For Each tag In tags
            expr.Expressions.Add(New OperationExpression() With {
                .Value = tag,
                .FilterOperation = FilterOperation.Equal,
                .PropertyName = PropertyName
            })
        Next tag
        Return expr
    End Function
    
  5. Add the created custom filter, ModelFilter, to the DataFilter control to filter the car models shown in the FlexGrid control.
    Dim _carsTable As DataTable
    Dim _dataProvider As New DataProvider()
    Dim _data As IEnumerable(Of car)
    
    Sub New()
    
        ' This call is required by the designer.
        InitializeComponent()
    
        _carsTable = _dataProvider.GetCarTable()
    
        Dim Data = New C1DataCollection(Of car)(_dataProvider.GetCarDataCollection(_carsTable))
    
        grid.ItemsSource = Data
        c1DataFilter.ItemsSource = Data
    
        Dim mdf = New ModelFilter()
    
        mdf.HeaderText = "Model"
        mdf.PropertyName = "Model"
        mdf.SetTagList(_dataProvider.GetCarDataCollection(_carsTable))
        c1DataFilter.Filters.Add(mdf)
    
    End Sub
    
    Private Sub c1DataFilter_FilterChanged(sender As Object, e As EventArgs)
        grid.ItemsSource = c1DataFilter.View.Cast(Of car)().ToList()
    
    End Sub
    

Similarly, you can create any other custom filter.