New to Telerik UI for WPF? Download free 30-day trial

Programmatic Filtering

RadVirtualGrid provides an API for programmatically filtering its data. The following article will go through the exposed mechanisms.

The FilteringControl of RadVirtualGrid can be manually closed by calling the CloseFilteringControl method of the control.

The events that this section covers cannot be utilized when the DataProvider mechanism is used for populating data. In such scenario, the built-in filtering can be altered through a Custom DataProvider.

RadVirtualGrid provides a set of events which can be utilized so that the filtering operation is aligned to the specific needs. They will be raised in the order they are listed below.

DataMemberNeeded

It is raised when the control is initially loaded and data related information for the given column is needed. It can be provided through the following properties that the event arguments expose.

  • ColumnIndex: Gives information regarding the index of the given column.

  • DataType: Gives information regarding the data type of the underlying member. The property can also be set.

  • IsFilterable: Provides information regarding whether the column generated for the given data member is filterable. The property can also be set.

  • IsSortable: Provides information regarding whether the column generated for the given data member is sortable. The property can also be set.

  • MemberName: Gives information regarding the name of the underlying data member. The property can also be set.

Example 1: Subscribing to the DataMemberNeeded event

private void VirtualGrid_DataMemberNeeded(object sender, DataMemberEventArgs e) 
    { 
        if (e.ColumnIndex == 0) 
        { 
            e.IsFilterable = true; 
            e.DataType = typeof(string); 
            e.MemberName = "Name"; 
        } 
    } 

DistinctValuesLoading

When the user clicks the filtering funnel, this event is raised as the FilteringControl needs to be populated with the relevant distinct values. Achieving this can be done through the properties of the event arguments.

  • Cancel: The event can be canceled by setting the property value to true.

  • ColumnIndex: Provides information regarding the index of the column for which the distinct values are needed.

  • ItemsSource: The property through which the distinct values required for the given column are provided.

Example 2: Subscribing to the DistinctValuesLoading event

private void VirtualGrid_DistinctValuesLoading(object sender, DistinctValuesLoadingEventArgs e) 
    { 
        if (e.ColumnIndex == 0) 
        { 
            e.ItemsSource = new List<string>() { "Chelsea", "Liverpool", "Arsenal" }; 
        } 
    } 

The end result after providing the needed distinct values will be as shown below.

Figure 1: RadVirtualGrid populated with distinct values

Opening the FilteringControl of RadVirtualGrid

FilterOperatorsLoading

After the required distinct values are loaded, the FilteringControl provides a mechanism its filter operators to be manipulated. The event arguments expose the following properties.

  • AvailableOperators: A readonly collection containing the available filter operators. It cannot be assigned and filter operators can only be removed from it.

  • ColumnIndex: Providing information regarding the index for the column for which the filter operators are being loaded.

  • DefaultFilterOperator1: Gets or sets the first default operator.

  • DefaultFilterOperator2: Gets or sets the second default operator.

Example 3: Subscribing to the FilterOperatorsLoading event

 private void VirtualGrid_FilterOperatorsLoading(object sender, FilterOperatorsLoadingEventArgs e) 
    { 
        if (e.ColumnIndex == 0) 
        { 
            e.DefaultOperator1 = Telerik.Windows.Data.FilterOperator.Contains; 
            e.DefaultOperator2 = Telerik.Windows.Data.FilterOperator.DoesNotContain; 
        } 
    } 

FieldFilterEditorCreated

Raised when the FilteringControl loads the field filter editor. It provides the ability the default filter editor to be customized. This can be achieved through the properties of the event arguments.

  • ColumnIndex: Gives information regarding the index of the column for which the filter editor has been created.

  • Editor: The default field filter editor can be accessed. Also, this is the property through which a custom filter editor can be applied.

Example 4: Subscribing to the FieldFilterEditorCreated event

private void VirtualGrid_FieldFilterEditorCreated(object sender, FieldFilterEditorCreatedEventArgs e) 
    { 
        if (e.ColumnIndex == 0) 
        { 
            var editor = new RadWatermarkTextBox(); 
            editor.WatermarkContent = "Enter value"; 
 
            Binding editorBinding = new Binding("Value") { UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged}; 
            BindingOperations.SetBinding(editor, RadWatermarkTextBox.TextProperty, editorBinding); 
 
            e.Editor = editor; 
        } 
    } 

FilterOperatorsPreparing

It is triggered when the filter operators are being prepared. Here is the state at which the filtering can be canceled if needed. This can be done by setting the Cancel property of the event arguments to __true_.

Example 5: Subscribing to the FilterOperatorsPreparing event

private void VirtualGrid_FilterDescriptorsPreparing(object sender, FilteringEventArgs e) 
    { 
        if (e.ColumnIndex == 0) 
        { 
            e.Cancel = true; 
        } 
    } 

FilterOperatorsPrepared

At this state the filter operators are ready and the filtering operation can be executed. The event arguments provide the properties listed below.

  • Added: A collection of the added filter descriptor.

  • ColumnFilterDescriptor: The filter descriptor generated for the given column.

  • ColumnIndex: The index for which the filter operators have been loaded.

  • Removed: The collection of removed filter operators.

Example 6: Subscribing to the FilterOperatorsPrepared event

private void VirtualGrid_FilterDescriptorsPrepared(object sender, FilteredEventArgs e) 
    { 
        if (e.ColumnIndex == 0) 
        { 
            CompositeFilterDescriptorCollection compositeFilterDescriptorCollection = new CompositeFilterDescriptorCollection(); 
            compositeFilterDescriptorCollection.Add(e.ColumnFilterDescriptor); 
            var clubs = clubsSource.AsQueryable().Where(compositeFilterDescriptorCollection).ToIList(); 
            this.VirtualGrid.InitialRowCount = clubs.Count; 
        } 
    } 

Note, that in the example above, the CompositeFilterDescriptorCollection is used for filtering the underlying data source. For this purpose, the Where extension method which is defined within the Telerik QueryableExtensions is utilized. More information can be found here. After the filtering is evaluated for the data source, the InitialRowCount of RadVirtualGrid needs to be set so that it equals the number of items that have passed the filtering criteria.

See Also

In this article