Child GridView selectedItems binding Error

4 Answers 144 Views
GridView
Psyduck
Top achievements
Rank 5
Bronze
Bronze
Bronze
Psyduck asked on 29 Jun 2021, 03:05 AM
Hello

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

Sort by
1
Dinko | Tech Support Engineer
Telerik team
answered on 06 Jul 2021, 11:00 AM

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.

0
Dinko | Tech Support Engineer
Telerik team
answered on 01 Jul 2021, 11:14 AM

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

0
Psyduck
Top achievements
Rank 5
Bronze
Bronze
Bronze
answered on 02 Jul 2021, 01:36 AM | edited on 02 Jul 2021, 01:43 AM

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.

0
Psyduck
Top achievements
Rank 5
Bronze
Bronze
Bronze
answered on 07 Jul 2021, 12:33 AM | edited on 07 Jul 2021, 12:34 AM

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.

Martin Ivanov
Telerik team
commented on 09 Jul 2021, 11:35 AM

The error appears because the local:GridViewSelectionUtilities.SelectedItems setting in the HierarchyChildTemplate points to a GridViewSubItems property of RadGridView, but the RadGridView control doesn't expose such property. To resolve this, you can remove the setting, or if you need it you can define such property in the GridViewSubModel class.
Psyduck
Top achievements
Rank 5
Bronze
Bronze
Bronze
commented on 12 Jul 2021, 04:31 AM

Sorry I didn't understand.

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?
Martin Ivanov
Telerik team
commented on 14 Jul 2021, 02:50 PM

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.

 

Psyduck
Top achievements
Rank 5
Bronze
Bronze
Bronze
commented on 15 Jul 2021, 02:48 AM

Gridview is bound from ViewModel to GridBaseModel.
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.
Martin Ivanov
Telerik team
commented on 19 Jul 2021, 03:33 PM | edited

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.

Psyduck
Top achievements
Rank 5
Bronze
Bronze
Bronze
commented on 20 Jul 2021, 12:58 AM

Thanks for the example, this solved it.

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.
Martin Ivanov
Telerik team
commented on 22 Jul 2021, 08:38 AM

I am glad to hear that the example helped. As for the dlls, no worries, and thanks for the understanding.
Tags
GridView
Asked by
Psyduck
Top achievements
Rank 5
Bronze
Bronze
Bronze
Answers by
Dinko | Tech Support Engineer
Telerik team
Psyduck
Top achievements
Rank 5
Bronze
Bronze
Bronze
Share this question
or