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
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.
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: