diff --git a/knowledge-base/grid-state-filter-nullable-bool.md b/knowledge-base/grid-state-filter-nullable-bool.md new file mode 100644 index 0000000000..981f0c1ef2 --- /dev/null +++ b/knowledge-base/grid-state-filter-nullable-bool.md @@ -0,0 +1,125 @@ +--- +title: Filter Nullable Bool Grid Column by Null Value +description: Learn how to filter Grid column bound to nullable boolean values programmatically. Discrover one of the many features of the Grid State. +type: how-to +page_title: How to Programatically Filter Nullable Bool Grid Column by Null Value +slug: grid-kb-filter-nullable-bool +tags: telerik, blazor, grid, filter, state +res_type: kb +ticketid: 1658561 +--- + +## Environment + + + + + + + + +
ProductGrid for Blazor
TreeList for Blazor
+ +## Description + +This KB article explains how to programmatically filter a Grid column bound to a `bool?` by the `null` values. + +## Solution + +To filter a Grid column bound to a `bool?` by the `null` values: + +1. Get the current [Grid state]({%slug grid-state%}). +1. Create a `CompositeFilterDescriptor` for the desired column. Add a child [`FilterDescriptor`]({%slug components/grid/filtering%}) with an `Operator` property that is equal to `FilterOperator.IsNull`. +1. Add the new `CompositeFilterDescriptor` to the `FilterDescriptors` property of the Grid state. +1. Use the Grid `SetStateAsync` method to update the Grid state. + +Alternative options are to: + +* Customize the Grid filter state in the [Grid `OnStateInit` event]({%slug grid-state%}#onstateinit). +* [Use a Grid filter template to enable filtering by `null` from the Grid's UI]({%slug grid-kb-filterrow-bool-customization%}). + +>caption Fitler bool? Grid column by null + +````CSHTML +@using Telerik.DataSource + +Filter On Leave By Null + + + + + + + + + + +@code { + private TelerikGrid? GridRef { get; set; } + + private List GridData { get; set; } = new(); + + private async Task SetGridFilter() + { + GridState currentState = GridRef!.GetState(); + + currentState.FilterDescriptors = new List() + { + new CompositeFilterDescriptor() + { + FilterDescriptors = new FilterDescriptorCollection() { + // Use the IsNull filter operator when filtering by null values. + new FilterDescriptor() + { + Member = nameof(SampleData.IsOnLeave), + MemberType = typeof(bool?), + Operator = FilterOperator.IsNull + } + } + } + }; + + await GridRef.SetStateAsync(currentState); + } + + protected override void OnInitialized() + { + GridData = Enumerable.Range(1, 30).Select(x => new SampleData + { + Id = x, + Name = $"Name {x}", + Team = $"Team {x % 4 + 1}", + IsOnLeave = GetRandomNullableBool(x) + }).ToList(); + } + + private bool? GetRandomNullableBool(int index) + { + if (index % 5 == 0) + { + return null; + } + + return Random.Shared.Next(2) == 0 ? false : true; + } + + public class SampleData + { + public int Id { get; set; } + public string Name { get; set; } = string.Empty; + public string Team { get; set; } = string.Empty; + public bool? IsOnLeave { get; set; } + public DateTime HireDate { get; set; } + } +} +```` + +## See Also + +* [FilterMenu: Filter From Code]({%slug grid-filter-menu%}#filter-from-code) +* [Working with the Grid State]({%slug grid-state%}) \ No newline at end of file