Programmatic Filtering
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.
Programmatic Filtering is achieved by adding different filter descriptors in the FilterDescriptor
collection of the .NET MAUI DataGrid control.
The following descriptor types are supported:
All FilterDescriptors
are located in the Telerik.Maui.Controls.Data
namespace.
When using C#, you'll need to add the using statement
using Telerik.Maui.Controls.Compatibility.Common.Data;
Alternatively, if using XAML, they're be resolved through the same telerik
xmlns:
xmlns:telerik="http://schemas.telerik.com/2022/xaml/maui"
Text Filter Descriptor
The TextFilterDescriptor
supports the following properties:
-
PropertyName
—Gets or sets the name of the property that is used to retrieve the filter value. -
Operator
—Gets or sets theTextOperator
value that defines how theValue
member is compared with each value from the items source. -
Value
—Gets or sets the value used in the comparisons. This is the right operand of the comparison. -
IsCaseSensitive
—Gets or sets a value that determines whether the text comparisons will be case-sensitive. The default value isTrue
.
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.
<telerik:TextFilterDescriptor PropertyName="Country"
Operator="StartsWith"
IsCaseSensitive="False"
Value="En"/>
Numerical Filter Descriptor
The NumericalFilterDescriptor
represents a descriptor which filters by properties of the numerical
data type.
It exposes the following properties:
-
PropertyName
—Gets or sets the name of the property that is used to retrieve the filter value. -
Value
—Gets or sets the value used in the comparisons. This is the right operand of the comparison. -
Operator
—Gets or sets theNumericalOperator
value that defines the boolean logic behind the left and right operand comparison.
<telerik:NumericalFilterDescriptor PropertyName="StadiumCapacity"
Operator="IsLessThan"
Value="80000"/>
DateTime Filter Descriptor
The DateTimeFilterDescriptor
is a descriptor which filters by properties of the System.DateTime
data type.
It exposes the following properties:
-
PropertyName
—Gets or sets the name of the property that is used to retrieve the filter value. -
Value
—Gets or sets the value used in the comparisons. This is the right operand of the comparison. -
Operator
—Gets or sets theNumericalOperator
value that defines the boolean logic behind the left and right operand comparison.
<telerik:DateTimeFilterDescriptor PropertyName="Established"
Operator="IsLessThan"
Value="1900/01/01"/>
TimeSpan Filter Descriptor
The TimeSpanFilterDescriptor
is a descriptor which filters by properties of the System.TimeSpan
data type.
It exposes the following properties:
-
PropertyName
—Gets or sets the name of the property that is used to retrieve the filter value. -
Value
—Gets or sets the value used in the comparisons. This is the right operand of the comparison. -
Operator
—Gets or sets theNumericalOperator
value that defines the boolean logic behind the left and right operand comparison.
<telerik:TimeSpanFilterDescriptor PropertyName="Time"
Operator="IsLessThan"
Value="22/11/21"/>
Boolean Filter Descriptor
The BooleanFilterDescriptor
is a descriptor which filters by properties of the System.Boolean
data type.
It exposes the following properties:
-
PropertyName
—Gets or sets the name of the property that is used to retrieve the filter value. -
Value
—Gets or sets the value used in the comparisons. This is the right operand of the comparison.
<telerik:BooleanFilterDescriptor PropertyName="IsChampion"
Value="true"/>
Nested Property Text Filter Descriptor
The NestedPropertyTextFilterDescriptor
is a descriptor which allows you to filter the nested properties.
It exposes the following properties:
-
PropertyName
—Gets or sets the name of the property that is used to retrieve the filter value. -
ItemPropertyGetter
—Sets a custom function that implements the access to the nested property. The function gets the value accessed by thePropertyName
as an input argument and returns the nested property as an output result. IfItemPropertyGetter
is not set, the filter descriptor behaves the same as TextFilterDescriptor. -
Operator
—Gets or sets theTextOperator
value that defines how theValue
member is compared with each value from the items source. -
Value
—Gets or sets the value used in the comparisons. This is the right operand of the comparison. -
IsCaseSensitive
—Gets or sets a value that determines whether the text comparisons will be case-sensitive. The default value isTrue
.
var filterDescriptor = new NestedPropertyTextFilterDescriptor();
filterDescriptor.PropertyName = "MyProperty";
filterDescriptor.Operator = Telerik.Maui.Controls.Data.TextOperator.EqualsTo;
filterDescriptor.Value = "Expected value";
filterDescriptor.ItemPropertyGetter = (originalPropertyValue) =>
{
// The 'originalPropertyValue' is the object fetched by the 'PropertyName' of the descriptor. In this case, this is the value of 'MyProperty'.
return ((MyChildObjectClass)originalPropertyValue).MyNestedProperty;
};
this.radDataGrid.FilterDescriptors.Add(filterDescriptor);
Distinct Values Filter Descriptor
The DistinctValuesFilterDescriptor
is a descriptor which filters by distinct values.
It exposes the following properties:
-
PropertyName
—Gets or sets the name of the property that is used to retrieve the filter value. -
Value
—Gets or sets the value used in the comparisons. This is the right operand of the comparison.
<telerik:DistinctValuesFilterDescriptor PropertyName="Country" Value="Austria" />
Composite Filter Descriptor
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.
<telerik:CompositeFilterDescriptor Operator="And">
<telerik:CompositeFilterDescriptor.Descriptors>
<telerik:NumericalFilterDescriptor PropertyName="StadiumCapacity"
Operator="IsGreaterThan"
Value="55000"/>
<telerik:NumericalFilterDescriptor PropertyName="StadiumCapacity"
Operator="IsLessThan"
Value="85000"/>
</telerik:CompositeFilterDescriptor.Descriptors>
</telerik:CompositeFilterDescriptor>
Delegate Filter Descriptor
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.
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.
Then, you need to add a DelegateFilterDescriptor
to the RadDataGrid.FilterDescriptors
collection and set its Filter
property.
The following example demonstrates the CustomFilter
implementation:
class CustomFilter : Telerik.Maui.Controls.Compatibility.Common.Data.IFilter
{
public bool PassesFilter(object item)
{
if(item is Club club
&& club.StadiumCapacity > 60000
&& club.StadiumCapacity < 85000)
{
return true;
}
else
{
return false;
}
}
}
IFilter
is inTelerik.Maui.Controls.Compatibility.Common.Data
namespace.
Add the DelegateFilterDescriptor
to the RadDataGrid
instance:
dataGrid.FilterDescriptors.Add(new DelegateFilterDescriptor() { Filter = new CustomFilter()});
For a runnable example with the DataGrid Programmatic Filtering scenario, see the SDKBrowser Demo Application and go to DataGrid > Filtering > Programmatic Filtering.