DataFilters / Data Binding
Data Binding

The DataFilter control can be bound to any data-aware control or to a model/custom collection. You can choose to bind the control using any of the following ways:

Binding with data-aware control

When you bind the DataFilter control with a data-aware control, fields are generated. On the basis of these fields present in the data source, different filters are automatically generated in the DataFilter control. These filters are BoolFilterRangeFilterDateRangeFilter and ChecklistFilter. However, the filtering criteria are not generated with these filters, for example, no minimum and maximum values are generated for RangeFilter and empty checklist is generated for ChecklistFilter. So, you need to customize the auto generated filters for defining the filtering criteria using FilterAutoGenerating event of the C1DataFilter class.

Model binding

  1. Define a class named 'Product' that returns the data which is to be bound to the DataFilter and DataGrid.
    Public Class Product
        Shared _rnd As Random = New Random()
        Shared _names As String() = "Macko|Surfair|Pocohey|Studeby".Split("|"c)
        Shared _lines As String() = "Computers|Washers|Stoves|Cars".Split("|"c)
        Shared _colors As String() = "Red|Green|Blue|White".Split("|"c)
    
        Public Sub New()
            Name = _names(_rnd.[Next]() Mod _names.Length)
            Line = _lines(_rnd.[Next]() Mod _lines.Length)
            Color = _colors(_rnd.[Next]() Mod _colors.Length)
            Price = 30 + _rnd.NextDouble() * 1000
            Cost = 3 + _rnd.NextDouble() * 300
            Discontinued = _rnd.NextDouble() < 0.2
            Introduced = DateTime.Today.AddDays(_rnd.[Next](-600, 0))
        End Sub
    
        Public Property Name As String
        Public Property Color As String
        Public Property Line As String
        Public Property Price As Double
        Public Property Cost As Double
        Public Property Introduced As DateTime
        Public Property Discontinued As Boolean
    End Class
    
  2. Assign a data source to the DataGrid and C1DataFilter. Also, set AutoGenerateFilters property to true.        
    'Assign datasource to the datagrid
    dataGrid.ItemsSource = _products
    
    'Assign datasource to the C1DataFilter
    c1DataFilter.ItemsSource = _products
    c1DataFilter.AutoGenerateFilters = True
    
           
  3. To customize the auto generated filters for defining the filtering criteria such as the minimum or maximum values for the RangeFilter and the checklist items for the CheckListFilter, subscribe to the FilterAutoGenerating event of DataFilter class as shown in the following code. Also, add the following code in the event handler of the FilterAutoGenerating event which provides the checklist items for the two filters namely “Name”, “Color” and sets the maximum and minimum value for the price filter.
    Private Sub C1DataFilter_FilterAutoGenerating(sender As Object, e As C1.DataFilter.FilterAutoGeneratingEventArgs)
        Select Case e.[Property].Name
            Case "Name"
                Dim brandFilter = CType(e.Filter, C1.WPF.DataFilter.ChecklistFilter)
                brandFilter.ItemsSource = _products
                brandFilter.ValueMemberPath = "Name"
                brandFilter.SelectAll()
            Case "Color"
                Dim categoryFilter = CType(e.Filter, C1.WPF.DataFilter.ChecklistFilter)
                categoryFilter.ItemsSource = _products
                categoryFilter.ValueMemberPath = "Color"
                categoryFilter.SelectAll()
            Case "Price"
                Dim priceFilter = CType(e.Filter, C1.WPF.DataFilter.RangeFilter)
                priceFilter.Maximum = _products.AsEnumerable().Max(Function(x) x.Price)
                priceFilter.Minimum = _products.AsEnumerable().Min(Function(x) x.Price)
                priceFilter.Increment = 1000
                priceFilter.Digits = 0
            Case Else
                e.Cancel = True
        End Select
    End Sub
    
  4. To update the data of the data-aware control, subscribe to the FilterChanged event of DataFilter as shown in the following code. Add the following code in the event handler of the FilterChanged event which reassigns data source to the FlexGrid control through View property of the C1DataFilter class.

    Private Sub C1DataFilter_FilterChanged(sender As Object, e As EventArgs)
    
        dataGrid.ItemsSource = c1DataFilter.View.Cast(Of Product)().ToList()
    
    End Sub
    

Back to Top