Hi,
i have a scenario where I as loggedIn user belongs to some Organization and in UsersView I can see all users and also one of the columns is Organization. I would like to set filter of this column to same Organization as Im in. So I see only users from my organization by default.
so my question is how to set this filter from code or can i somehow bind it from xaml to some property ?
1 Answer, 1 is accepted
Hi John,
DataGrid Filtering is described here: https://docs.telerik.com/devtools/maui/controls/datagrid/filtering/overview
If you do not want to filter the data through the UI, you can use programmatic filtering: https://docs.telerik.com/devtools/maui/controls/datagrid/filtering/programmatic-filtering
<telerik:RadDataGrid x:Name="dataGrid">
<telerik:RadDataGrid.FilterDescriptors>
<telerik:TextFilterDescriptor PropertyName="Country" Operator="StartsWith" Value="I" />
</telerik:RadDataGrid.FilterDescriptors>
</telerik:RadDataGrid>
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
this.dataGrid.ItemsSource = new List<Data>
{
new Data { Country = "India", Capital = "New Delhi"},
new Data { Country = "India", Capital = "New Delhi"},
new Data { Country = "South Africa", Capital = "Cape Town"},
new Data { Country = "Nigeria", Capital = "Abuja" },
new Data { Country = "Singapore", Capital = "Singapore" }
};
// or setting the filter in code:
this.dataGrid.FilterDescriptors.Add(new TextFilterDescriptor
{
PropertyName = "Country",
Operator = TextOperator.StartsWith,
Value = "I"
});
}
}
public class Data
{
public string Country { get; set; }
public string Capital { get; set; }
}
Or you can use a DelegateFilterDescriptor: https://docs.telerik.com/devtools/maui/controls/datagrid/filtering/programmatic-filtering#delegate-filter-descriptor
Regards,
Didi
Progress Telerik
Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.
and can I use binding in Value ?
and also would it be possible to check checkboxes in filter instead of adding text filter ?
[I've converted your question from "Answer" to "Comment"]
Hi John,
Okay, now to answer your question... No, you cannot. This is because the Value property is a CLR property, not a .NET MAUI DependencyProperty.
Direct Option
If you want to dynamically create a TextFilterDescriptor, you can do it in the code behind like this:
MyGrid.FilterDescriptors.Clear();
var newFilter = new TextFilterDescriptor { Value = "MyValue", PropertyName = "MyPropertyName", Operator = TextOperator.EqualsTo };
MyGrid.FilterDescriptors.Add(newFilter);
MVVM Option
Or, if this filter selection logic needs to happen int the view model, you can OneWayToSource bind the DataGrid's FilterDescriptors collection, then operate on that collection in the view model
For example:
<telerik:RadDataGrid FilterDescriptors="{Binding VmFilterDescriptors, Mode=OneWayToSource}" ...>
</telerik:RadDataGrid>
and in the view model, you work with the FilterDescriptorCollection in the same way (because the "VmFilterDescriptors" view model property is a direct reference to the DataGrid's FilterDescriptors property)
public class ViewModel
{
...
public FilterDescriptorCollection VmFilterDescriptors { get; set; }
public string MyProperty { get; set; }
public string MyFilterProperty { get; set; }
private void DoSomethingWithFilters()
{
VmFilterDescriptors.Clear();
var newFilter = new TextFilterDescriptor
{
Value = MyFilterProperty,
PropertyName = MyProperty,
Operator = TextOperator.EqualsTo
};
VmFilterDescriptors.Add(newFilter);
}
}
Side Note: My code above is illustrative to communicate the point, you will need to tweak it to match your exact needs. I hope this helps, if you still have trouble, then this is likely a non-public concern and unsuited for the forums, instead please open a Support Ticket so that we can assist directly with your private code.
[Converted Answer to Comment]
Hi John, I see you're accidentally adding answers to this thread when asking a new question. this is not an uncommon mistake, here's the reply button for
Okay, so back to your question (my apologies, I missed answering that 2nd part of your question more specifically).
If you want to programmatically interact with the filter/sorting/grouping system of the DataGrid programmatically, then you need to use the FilterDescriptors, SortDescriptors and GroupDescriptors collections with Add(), Remove() and Clear() methods.
When the user checks those checkboxes, it literally the same thing as adding a filter descriptor (except we are doing it for you). So, the programmatic equivalent of what is in that screenshot is exactly this:
MyGrid.FilterDescriptors.Add(new TextFilterDescriptor { Value = "Value 1", ... });
MyGrid.FilterDescriptors.Add(new TextFilterDescriptor { Value = "Value 2", ... });
Custom "CheckAll"
Are you concerned with having your own programmatic version of the CheckAll checkbox? Then you need to do the same thing we do, and iterate over all the rows int he data and create separate FilterDescriptor for each distinct value.
Here's an example:
private void CheckAll()
{
// Create a cache to hold a list of the filters you want to add
var descriptors = new List<TextFilterDescriptor>();
// Iterate over the visible items in the dataGrid (this can be done through the DataView)
foreach (var dataViewItem in MyDataGrid.GetDataView().Items)
{
var person = dataViewItem as Person;
descriptors.Add(new TextFilterDescriptor
{
// I am using the value of the item
Value = person.Name,
// Set the property name
PropertyName = nameof(Person.Name),
// Choose the operator
Operator = TextOperator.EqualsTo
});
}
// Now that we have a list fo the filters we want to apply, we can do it all at once
foreach (var textFilterDescriptor in descriptors)
{
MyDataGrid.FilterDescriptors.Add(textFilterDescriptor);
}
}
Note: The above example doesn't really make sense, because the filter results would be the same as what is currently visible, but it gets the point across on how to build up a list of filters before applying them