To use SelectedItems, "GridViewSelectionUtilities" of Telerik GridView SDK was used.
A normal gridview works just fine.
Here, the Model adds another hierarchical gridview. and run.
Multiselecting the parent gridview works fine.
But when I select the child gridview, an error appears.
An exception was thrown because the model format is different.
"Cannot be used for this generic collection."
How do I solve this?
I need to get the SelectedItmes of both the parent GridView and the child Gridview.
Thanks.
4 Answers, 1 is accepted
Hi KIM,
Thank you for the provided project.
I have made a few modifications to the project to make it run. I created new sub data for each row, otherwise selecting an item from one child grid will select the same one inside another child grid. Now every object derives from one base class. You can set the SelectedItems collection to this base class. Also, I have set the attached property to the child grid inside the HierarchyChildTemplate.
Give this project a try and let me know how it goes.
Regards,
Dinko
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.
Hi Psyduck,
Thank you for the provided project.
What you can do is to subscribe to the DataLoading event of the RadGridView. Inside the event handler, you can get the child grid and bind the custom attached property. The following code snippet demonstrates what I have in mind.
private void RadGridView_DataLoading(object sender, Telerik.Windows.Controls.GridView.GridViewDataLoadingEventArgs e)
{
GridViewDataControl dataControl = (GridViewDataControl)sender;
if (dataControl.ParentRow != null)
{
if(GridViewSelectionUtilities.GetSelectedItems(dataControl) == null)
{
Binding binding = new Binding("SelectedItems");
binding.Source = this.DataContext;
dataControl.SetBinding(GridViewSelectionUtilities.SelectedItemsProperty, binding);
}
}
}
Regards,
Dinko
Progress Telerik
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Hello
I still can't solve it and I get the same error.
private static void GridView_SelectionChanged(object sender, SelectionChangeEventArgs args)
{
...
foreach (object item in args.AddedItems)
{
collection.Add(item); // << Cannot be used for this generic collection.
}
}
I got the same error no matter what I put in the example you provided.
sub 1. <telerik:RadGridView ItemsSource="{Binding GridViewSubs}" local:GridViewSelectionUtilities.SelectedItems="{Binding GridViewSubItems}">
sub 2. <telerik:RadGridView ItemsSource="{Binding GridViewSubs}" local:GridViewSelectionUtilities.SelectedItems="{Binding GridViewSubItems, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=telerik:RadGridView}}"
private void RadGridView_DataLoading(object sender, Telerik.Windows.Controls.GridView.GridViewDataLoadingEventArgs e)
{
GridViewDataControl dataControl = (GridViewDataControl)sender;
if (dataControl.ParentRow != null)
{
if (GridViewSelectionUtilities.GetSelectedItems(dataControl) == null)
{
Binding binding = new Binding("SelectedItems");
binding.Source = this.DataContext;
//binding.Source = (this.DataContext as ViewModel).GridViewMain;
//binding.Source = (this.DataContext as ViewModel).GridViewMainItems;
//binding.Source = (this.DataContext as ViewModel).GridViewSubItems;
dataControl.SetBinding(GridViewSelectionUtilities.SelectedItemsProperty, binding);
}
}
}
It didn't work even if I put another variable in the place where I made the comment and used it.
(Other than that, I've added other things, but the same error occurs.)
What am I doing wrong?
Thanks.

Hello.
This is not what I thought it would be, but in the end an error appears but it works.
Is it okay to create derived objects with one base class like this?
(Subclass properties are also included in the base class, but you should not use them in the main model.)
This will result in a binding error when you click the first "+".
System.Windows.Data Error: 40 : BindingExpression path error: 'GridViewSubItems' property not found on 'object' ''RadGridView' (Name='')'. BindingExpression:Path=GridViewSubItems; DataItem='RadGridView' (Name=''); target element is 'RadGridView' (Name=''); target property is 'SelectedItems' (type 'INotifyCollectionChanged')
Thanks.
I need both. Parent gridview SelectedItems and child gridview SelectedItems
Therefore, the setting should not be removed.
What does it mean to define a property in a submodel class?
Does the last provided Dinko's source need new settings?
The project provided by Dinko has three sets of models. The ViewModel class which is the data context of the MainWindow view. GridViewMainModel for the root level of the RadGridView rows. And GridViewSubModel for the sub level rows (coming from the HierarchyChildTemplate).
The parent RadGridView in the MainWindow's root Grid panel is bound to the ViewModel object which contains a property named SelectedItems. On the other hand the data context of the RadGridView in the HierarchyChildTemplate is the parent row's underlying data item. This is an object of type GridViewMainModel. The child RadGridView's selection is bound to the GridViewSubs which is not presented in the GridViewSubModel model. This is why the error appears.
In order to achieve your requirement, you can add such property in the GridViewSubModel class and use it to handle the selection of the child gridview.
The child gridview (SubModel) is bound to GridMainModel.
This is why the error appears because it is not the top level item.
Is my understanding correct?
You said to add a property to solve this.
What property am I adding?
I put public ObservableCollection<GridViewSubModel> GridViewSubItems in each ViewModel/BaseModel/MainModel/SubModel and tried to run them, but I got the same error result.
Yes, your understanding is correct. As for the property, I've updated Dinko's project to show my idea. I hope that helps.
Also, note that I've replaced the original project from this forum with the same one but without Telerik dlls. This is because it was containing a licensed version of the Telerik assemblies. Please avoid uploading those on public websites.
Sorry about the Telerik dll. In the future, I will remove it and upload it.
I've never posted it on a public website except here.
I'll be careful.