|
| 1 | +--- |
| 2 | +title: Programmatic Filtering |
| 3 | +page_title: .NET MAUI TreeDataGrid Documentation - Programmatic Filtering |
| 4 | +description: Learn how to apply an external filter to the TreeDataGrid for .NET MAUI by disabling the built-in filtering UI and programmatically filter tha data in the tree grid. |
| 5 | +position: 3 |
| 6 | +slug: treedatagrid-programmatic-filtering |
| 7 | +--- |
| 8 | + |
| 9 | +# Programmatic Filtering |
| 10 | + |
| 11 | +Programmatic Filtering can be used for external filtering. For example, disable the built-in filtering UI and to filter the data in the grid programmatically. |
| 12 | +Programmatic Filtering is achieved by adding different filter descriptors in the `FilterDescriptor` collection of the [.NET MAUI TreeDataGrid]({%slug treedatagrid-overview%}) control. |
| 13 | + |
| 14 | +The following descriptor types are supported: |
| 15 | + |
| 16 | +- [.NET MAUI TreeDataGrid Filtering](#net-maui-treedatagrid-filtering) |
| 17 | + - [Filtering UI](#filtering-ui) |
| 18 | + - [FilterControl Template](#filtercontrol-template) |
| 19 | + - [Programmatic Filtering](#programmatic-filtering) |
| 20 | + - [Text Filter Descriptor](#text-filter-descriptor) |
| 21 | + - [Numerical Filter Descriptor](#numerical-filter-descriptor) |
| 22 | + - [DateTime Filter Descriptor](#datetime-filter-descriptor) |
| 23 | + - [TimeSpan Filter Descriptor](#timespan-filter-descriptor) |
| 24 | + - [Boolean Filter Descriptor](#boolean-filter-descriptor) |
| 25 | + - [Nested Property Text Filter Descriptor](#nested-property-text-filter-descriptor) |
| 26 | + - [Distinct Values Filter Descriptor](#distinct-values-filter-descriptor) |
| 27 | + - [Composite Filter Descriptor](#composite-filter-descriptor) |
| 28 | + - [Delegate Filter Descriptor](#delegate-filter-descriptor) |
| 29 | + - [See Also](#see-also) |
| 30 | + |
| 31 | +All `FilterDescriptors` are located in the `Telerik.Maui.Controls.Data` namespace. |
| 32 | + |
| 33 | +When using C#, you'll need to add the using statement |
| 34 | + |
| 35 | +```C# |
| 36 | +using Telerik.Maui.Controls.Data; |
| 37 | +``` |
| 38 | + |
| 39 | +Alternatively, if using XAML, they're be resolved through the same `telerik` xmlns: |
| 40 | + |
| 41 | +```XAML |
| 42 | +xmlns:telerik="http://schemas.telerik.com/2022/xaml/maui" |
| 43 | +``` |
| 44 | + |
| 45 | +### Text Filter Descriptor |
| 46 | + |
| 47 | +The `TextFilterDescriptor` supports the following properties: |
| 48 | + |
| 49 | +* `PropertyName`—Gets or sets the name of the property that is used to retrieve the filter value. |
| 50 | +* `Operator`—Gets or sets the `TextOperator` value that defines how the `Value` member is compared with each value from the items source. |
| 51 | +* `Value`—Gets or sets the value used in the comparisons. This is the right operand of the comparison. |
| 52 | +* `IsCaseSensitive`—Gets or sets a value that determines whether the text comparisons will be case-sensitive. The default value is `True`. |
| 53 | + |
| 54 | +To use `TextFilterDescriptor`, you need to add its instance to the `RadDataGrid.FilterDescriptors` collection and to set its `PropertyName` property to associate it with the property from your custom objects. Then, through the `Operator` and `Value` properties, you need to set the filter condition and the value to compare. You can also use the `IsCaseSensitive` property to determine if the text comparisons will be case-sensitive or not. |
| 55 | + |
| 56 | +```XAML |
| 57 | +<telerik:TextFilterDescriptor PropertyName="Country" |
| 58 | + Operator="StartsWith" |
| 59 | + IsCaseSensitive="False" |
| 60 | + Value="En"/> |
| 61 | +``` |
| 62 | + |
| 63 | +### Numerical Filter Descriptor |
| 64 | + |
| 65 | +The `NumericalFilterDescriptor` represents a descriptor which filters by properties of the `numerical` data type. |
| 66 | + |
| 67 | +It exposes the following properties: |
| 68 | + |
| 69 | +* `PropertyName`—Gets or sets the name of the property that is used to retrieve the filter value. |
| 70 | +* `Value`—Gets or sets the value used in the comparisons. This is the right operand of the comparison. |
| 71 | +* `Operator`—Gets or sets the `NumericalOperator` value that defines the boolean logic behind the left and right operand comparison. |
| 72 | + |
| 73 | +```XAML |
| 74 | +<telerik:NumericalFilterDescriptor PropertyName="StadiumCapacity" |
| 75 | + Operator="IsLessThan" |
| 76 | + Value="80000"/> |
| 77 | +``` |
| 78 | + |
| 79 | +### DateTime Filter Descriptor |
| 80 | + |
| 81 | +The `DateTimeFilterDescriptor` is a descriptor which filters by properties of the `System.DateTime` data type. |
| 82 | + |
| 83 | +It exposes the following properties: |
| 84 | + |
| 85 | +* `PropertyName`—Gets or sets the name of the property that is used to retrieve the filter value. |
| 86 | +* `Value`—Gets or sets the value used in the comparisons. This is the right operand of the comparison. |
| 87 | +* `Operator`—Gets or sets the `NumericalOperator` value that defines the boolean logic behind the left and right operand comparison. |
| 88 | + |
| 89 | +```XAML |
| 90 | +<telerik:DateTimeFilterDescriptor PropertyName="Established" |
| 91 | + Operator="IsLessThan" |
| 92 | + Value="1900/01/01"/> |
| 93 | +``` |
| 94 | + |
| 95 | +### TimeSpan Filter Descriptor |
| 96 | + |
| 97 | +The `TimeSpanFilterDescriptor` is a descriptor which filters by properties of the `System.TimeSpan` data type. |
| 98 | + |
| 99 | +It exposes the following properties: |
| 100 | + |
| 101 | +* `PropertyName`—Gets or sets the name of the property that is used to retrieve the filter value. |
| 102 | +* `Value`—Gets or sets the value used in the comparisons. This is the right operand of the comparison. |
| 103 | +* `Operator`—Gets or sets the `NumericalOperator` value that defines the boolean logic behind the left and right operand comparison. |
| 104 | + |
| 105 | +```XAML |
| 106 | +<telerik:TimeSpanFilterDescriptor PropertyName="Time" |
| 107 | + Operator="IsLessThan" |
| 108 | + Value="22/11/21"/> |
| 109 | +``` |
| 110 | + |
| 111 | +### Boolean Filter Descriptor |
| 112 | + |
| 113 | +The `BooleanFilterDescriptor` is a descriptor which filters by properties of the `System.Boolean` data type. |
| 114 | + |
| 115 | +It exposes the following properties: |
| 116 | + |
| 117 | +* `PropertyName`—Gets or sets the name of the property that is used to retrieve the filter value. |
| 118 | +* `Value`—Gets or sets the value used in the comparisons. This is the right operand of the comparison. |
| 119 | + |
| 120 | +```XAML |
| 121 | +<telerik:BooleanFilterDescriptor PropertyName="IsChampion" |
| 122 | + Value="true"/> |
| 123 | +``` |
| 124 | + |
| 125 | +### Nested Property Text Filter Descriptor |
| 126 | + |
| 127 | +The `NestedPropertyTextFilterDescriptor` is a descriptor which allows you to filter the nested properties. |
| 128 | + |
| 129 | +It exposes the following properties: |
| 130 | + |
| 131 | +* `PropertyName`—Gets or sets the name of the property that is used to retrieve the filter value. |
| 132 | +* `ItemPropertyGetter`—Sets a custom function that implements the access to the nested property. The function gets the value accessed by the `PropertyName` as an input argument and returns the nested property as an output result. If `ItemPropertyGetter` is not set, the filter descriptor behaves the same as [TextFilterDescriptor](#text-filter-descriptor). |
| 133 | +* `Operator`—Gets or sets the `TextOperator` value that defines how the `Value` member is compared with each value from the items source. |
| 134 | +* `Value`—Gets or sets the value used in the comparisons. This is the right operand of the comparison. |
| 135 | +* `IsCaseSensitive`—Gets or sets a value that determines whether the text comparisons will be case-sensitive. The default value is `True`. |
| 136 | + |
| 137 | +```C# |
| 138 | +var filterDescriptor = new NestedPropertyTextFilterDescriptor(); |
| 139 | +filterDescriptor.PropertyName = "MyProperty"; |
| 140 | +filterDescriptor.Operator = Telerik.Maui.Controls.Data.TextOperator.EqualsTo; |
| 141 | +filterDescriptor.Value = "Expected value"; |
| 142 | +filterDescriptor.ItemPropertyGetter = (originalPropertyValue) => |
| 143 | +{ |
| 144 | + // The 'originalPropertyValue' is the object fetched by the 'PropertyName' of the descriptor. In this case, this is the value of 'MyProperty'. |
| 145 | + return ((MyChildObjectClass)originalPropertyValue).MyNestedProperty; |
| 146 | +}; |
| 147 | +this.radTreeDataGrid.FilterDescriptors.Add(filterDescriptor); |
| 148 | +``` |
| 149 | + |
| 150 | +### Distinct Values Filter Descriptor |
| 151 | + |
| 152 | +The `DistinctValuesFilterDescriptor` is a descriptor which filters by distinct values. |
| 153 | + |
| 154 | +It exposes the following properties: |
| 155 | + |
| 156 | +* `PropertyName`—Gets or sets the name of the property that is used to retrieve the filter value. |
| 157 | +* `Value`—Gets or sets the value used in the comparisons. This is the right operand of the comparison. |
| 158 | + |
| 159 | +```XAML |
| 160 | +<telerik:DistinctValuesFilterDescriptor PropertyName="Country" Value="Austria" /> |
| 161 | +``` |
| 162 | + |
| 163 | +### Composite Filter Descriptor |
| 164 | + |
| 165 | +The `CompositeFilterDescriptor` represents a special `FilterDescriptorBase` that stores an arbitrary number of other descriptors instances. The logical `AND` or `OR` operator is applied upon all composed filters to determine the result of the `PassesFilter` routine. |
| 166 | + |
| 167 | +```XAML |
| 168 | +<telerik:CompositeFilterDescriptor Operator="And"> |
| 169 | + <telerik:CompositeFilterDescriptor.Descriptors> |
| 170 | + <telerik:NumericalFilterDescriptor PropertyName="StadiumCapacity" |
| 171 | + Operator="IsGreaterThan" |
| 172 | + Value="55000"/> |
| 173 | + <telerik:NumericalFilterDescriptor PropertyName="StadiumCapacity" |
| 174 | + Operator="IsLessThan" |
| 175 | + Value="85000"/> |
| 176 | + </telerik:CompositeFilterDescriptor.Descriptors> |
| 177 | +</telerik:CompositeFilterDescriptor> |
| 178 | +``` |
| 179 | + |
| 180 | +### Delegate Filter Descriptor |
| 181 | + |
| 182 | +The `DelegateFilterDescriptor` exposes the `Filter` property, which gets or sets the `IFilter` implementation used to check whether a data item passes the filter or not. |
| 183 | + |
| 184 | +To use a `DelegateFilterDescriptor`, you need to create a class that implements the `IFilter` interface which will return the `Key` by which you want to filter. |
| 185 | + |
| 186 | +Then, you need to add a `DelegateFilterDescriptor` to the `RadDataGrid.FilterDescriptors` collection and set its `Filter` property. |
| 187 | + |
| 188 | +The following example demonstrates the `CustomFilter` implementation: |
| 189 | + |
| 190 | +```C# |
| 191 | +class CustomFilter : Telerik.Maui.Controls.Data.IFilter |
| 192 | +{ |
| 193 | + public bool PassesFilter(object item) |
| 194 | + { |
| 195 | + if(item is Club club |
| 196 | + && club.StadiumCapacity > 60000 |
| 197 | + && club.StadiumCapacity < 85000) |
| 198 | + { |
| 199 | + return true; |
| 200 | + } |
| 201 | + else |
| 202 | + { |
| 203 | + return false; |
| 204 | + } |
| 205 | + } |
| 206 | +} |
| 207 | +``` |
| 208 | + |
| 209 | +> `IFilter` is in `Telerik.Maui.Controls.Data` namespace. |
| 210 | +
|
| 211 | +Add the `DelegateFilterDescriptor` to the `RadDataGrid` instance: |
| 212 | + |
| 213 | +```C# |
| 214 | +treeDataGrid.FilterDescriptors.Add(new DelegateFilterDescriptor() { Filter = new CustomFilter()}); |
| 215 | +``` |
| 216 | + |
| 217 | +>important As the TreeDataGrid inherits from DataGrid, you can review the Programmatic Filtering scenario, see the [SDKBrowser Demo Application]({%slug sdkbrowser-app%}) and go to **DataGrid > Filtering > Programmatic Filtering**. |
| 218 | +
|
| 219 | +## See Also |
| 220 | + |
| 221 | +- [Filtering UI]({%slug treedatagrid-filtering-ui%}) |
| 222 | +- [Filter Control Template]({%slug treedatagrid-filter-control-template%}) |
| 223 | +- [Programmatic Filtering]({%slug treedatagrid-programmatic-filtering%}) |
0 commit comments