DataGrid propety SelectedItems doesn't exist

1 Answer 14 Views
DataGrid
Yurii
Top achievements
Rank 1
Yurii asked on 08 Apr 2024, 11:02 AM
Good afternoon, can you please advise why the documentation states: "SelectedItems.
The DataGrid exposes the SelectedItem and SelectedItems properties which you can use depending on whether you have defined a single or multiple selection mode.", but in practice there is no SelectedItems property.

1 Answer, 1 is accepted

Sort by
1
Accepted
Yana
Telerik team
answered on 09 Apr 2024, 10:32 AM

Hi Yurii,

The DataGrid exposes SelectedItems collection which is read-only, that means you cannot set it, you can just add and remove items from it. I guess you tried to set it in XAML and that's why you didn't manage to find it.

You can easily access it with code, for example, this is how you can remove the first item from the current selection (in case of multiple selection):

if (this.dataGrid.SelectedItems.Count > 0)
{
    this.dataGrid.SelectedItems.RemoveAt(0);
}

I hope I was of help.  If you have any additional questions related to the SelectedItems, please send me more details on the exact use case you have, so I can provide more precise assistance.

Regards,
Yana
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.

Yurii
Top achievements
Rank 1
commented on 11 Apr 2024, 06:36 PM

Yana, thank you very much for your reply, could you please advise how the SelectedItems property can be used from an MVVM perspective?
Yana
Telerik team
commented on 12 Apr 2024, 11:20 AM

Hi Yurii,

Yes, I am sorry I missed that.  You can still bind SelectedItems to a collection of type ObservableCollection<object> and receive notifications for selected items changes through its CollectionChanged event. Here is a quick example:

Let's have the following sample DataGrid definition:

<telerik:RadDataGrid x:Name="dataGrid"
                        ItemsSource="{Binding Stores}"
                        AutoGenerateColumns="True"
                        SelectionUnit="Row"
                        SelectionMode="Multiple"
                        SelectedItems="{Binding SelectedStores}"
                        Grid.Row="1" />
Here is the ViewModel class where the SelectedStores is defined:

public class ViewModel:NotifyPropertyChangedBase
{
    private ObservableCollection<object> selectedStores;
    public ViewModel()
    {
        this.Stores = this.GenerateStores(20);
    }
    public ObservableCollection<Store> Stores { get; set; }

    public ObservableCollection<object> SelectedStores
    {
        get
        {
            return this.selectedStores;
        }
        set
        {
            if (this.selectedStores != value)
            {

                if (this.selectedStores != null)
                {
                    this.selectedStores.CollectionChanged -= SelectedStores_CollectionChanged;
                }

                this.selectedStores = value;

                if (this.selectedStores != null)
                {
                    this.selectedStores.CollectionChanged += SelectedStores_CollectionChanged;
                }

                OnPropertyChanged();
            }
        }
    }

    private void SelectedStores_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
        {
            var newSelectedItems = e.NewItems[0];
            var currentlySelectedItemsCount = this.SelectedStores.Count();
        }
    }
}

Inside the CollectionChanged event handler you can get all the selected items, as well as the newly added or removed.

Please give this a try and let me know if you have any additional questions or concerns.

Tags
DataGrid
Asked by
Yurii
Top achievements
Rank 1
Answers by
Yana
Telerik team
Share this question
or