[Solved] Access filtered and selected items in RadDataGrid

1 Answer 131 Views
DataGrid
Michel
Top achievements
Rank 1
Michel asked on 24 Jun 2025, 03:13 PM

I am using a RadDataGrid with SelectionMode set to Multiple. I have bound the SelectedItems to an observableCollection and this al works fine. I have enable filtering on colums and with the SearchPanel which works fine.
I can now selected multiple items and then apply a filter. Now I would like to access all items that are filtered and selected. The SelectedItems contains all selected items, filtered and unfiltered but only want the filtered (+ selected) ones.

Is there a property that contains the filtered items so I can intersect it with the SelectedItems?

If not can I apply the filters to the ItemsSource my self and get the filtered items this way?

Is it possible to get the list of filtered AND selected items any other (simple) way?

 

1 Answer, 1 is accepted

Sort by
0
Accepted
Lance | Senior Manager Technical Support
Telerik team
answered on 24 Jun 2025, 04:18 PM

Hi Michel,

What you are looking for is known as the internal "DataView", you can get a reference to that DataView by using the GetDataView method:

var dataView = this.dataGrid.GetDataView();

In short, the GetDataView method returns an IDataViewCollection object, which contains a view of the ItemsSource after all the sorting, grouping, and filtering operations are applied.

You can use that view for your additional items processing.

Regards,
Lance | Senior Manager Technical Support
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.

Michel
Top achievements
Rank 1
commented on 25 Jun 2025, 05:26 AM

Hi Lance,

Thank you for your quick response. The dataView.Items is the list of filtered items I was looking for. This solves my problem.

The only minor disadvantage is that I need to do this in code behind while I prefer to keep all functionality in my viewmodel. But most important is that it works.

Lance | Senior Manager Technical Support
Telerik team
commented on 25 Jun 2025, 02:14 PM

I'm happy to hear that works!

MVVM Friendly Approach

As for sticking to pure MVVM, you can create an interface where the only the ViewModel does the logic while maintaining IoC.

I had to do this when I built the Xamarin.Forms CRM demo's DataGrid features, let me show the parts that you can use for inspiration, then I will write you a version for MAUI.

  1. To start, go to master/ArtGalleryCRM 
  2. Next, drill down to the Forms project, where the IU code is ArtGalleryCRM.Forms 
  3. Take a look at this interface Interfaces/IDataGridView.cs 
    • I defined those methods so they could be invoked from the view model, without requiring a hard reference to any specific view)
  4. Next, lets look at the view model, I have a property of the interface's type OrdersViewModel.cs#L50 , and to invoke those methods, see OrdersViewModel.cs#L90-L91 
  5. Finally, look at the view where I have a DataGrid Views/OrderPages/OrdersPage.xaml.cs 
    •  Notice I implement the interface, and add the code needed for each method:

For You

Okay, with that walkthrough done, let's make this work for your needs. In your project, you will want to follow the same idea, except your interface's method would only be to call for the DataView and it returns a type of IDataViewCollection.

The interface:

public interface IDataGridView
{
    IDataViewCollection GetDataView();
}

The View Model

public class MainViewModel : ViewModelBase
{
    private ObservableRangeCollection<Employee> employees;

    public MainViewModel()
    {
        Employees = new ObservableRangeCollection<Employee>(SampleDataService.Current.GenerateEmployeeData());
    }

    public ObservableRangeCollection<Employee> Employees
    {
        get => employees;
        set => SetProperty(ref employees, value);
    }

    public IDataGridView DataGridView { get; set; }

 ...
}

The View

public partial class MainPage : ContentPage, IDataGridView
{
    public MainPage(MainViewModel vm)
    {
        InitializeComponent();
        this.BindingContext = vm;

        // Identify this view for the IoC reference
        vm.DataGridView = this;
    }

    public IDataViewCollection GetDataView()
    {
        return EmployeesGrid.GetDataView();
    }
}

With that configured, you can call the method anywhere in the view model in a safe manner, without a hard reference to the view that breaks MVVM!

One word of advice... because it takes time for the operations to complete on the DataView (filtering/sorting/grouping), you should always check that the data is ready before attempting to read the value of Items. I recommend something like this while loop that waits until the Dataview is ready:

Real Demo

I have updated my DevOpsExamples repo MAUI project to implement this interface and approach so you can see it in action:

Go to MainPage and its view model to see the implementation => https://github.com/LanceMcCarthy/DevOpsExamples/tree/main/src/MAUI 

Tags
DataGrid
Asked by
Michel
Top achievements
Rank 1
Answers by
Lance | Senior Manager Technical Support
Telerik team
Share this question
or