This is a migrated thread and some comments may be shown as answers.

Binding to generic lists

5 Answers 87 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Michael Fooks
Top achievements
Rank 1
Michael Fooks asked on 04 Jan 2010, 06:19 AM
I'm trying to bind the gridview to an ObservableCollection<MyListItem>. In my collection I have a mixture of items that inherit from MyListItem e.g. MyAppleItem and MyOrangeItem.  Fields bound in the grid are bound to properties exposed in the base MyListItem type. The binding works fine when the colleciton contains a list with a single type in it but as soon as I have a collection with some MyAppleItem and MyOrangeItem I get an argument exception:

{"The value \"MyAppleItem\" is not of type \"MyOrangeItem\" and cannot be used in this generic collection.\r\nParameter name: value"}

I need the items in their original types as I use a converter on one of the columns etc.  How do I make use of polymorphism with the grid binding?

5 Answers, 1 is accepted

Sort by
0
Stefan Dobrev
Telerik team
answered on 04 Jan 2010, 09:11 AM
Hello Bronwen,

We have fixed similar issue in one of latest internal builds. Can you please upgrade your binaries to the ones from the latest internal build and verify if the exception is still thrown?

All the best,
Stefan Dobrev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Michael Fooks
Top achievements
Rank 1
answered on 04 Jan 2010, 10:59 PM
Hi,
I tried the 2009.3.1219.35 dlls and still get the same exception.
Regards,
Bronwen
0
Stefan Dobrev
Telerik team
answered on 05 Jan 2010, 07:36 AM
Hi Bronwen,

I thing I figure out the problem. It is due to the fact that the grid resolve its elements' type based on the actual elements in the collection. In your case when you have just oranges we resolve the element type as MyOrangeItem, not as MyListItem. Thus when you add a new item of type MyAppleItem we try to treat it as MyOrangeItem. We will remove this limitation in one of our next releases of the RadGridView.

As an immediate workaround I can suggest you to clear the ItemsSource of the grid and the reassign it again after you have added an item of different type to your collection. This way the grid will understand that there are items of different types in the collection and correctly resolve its element type to your base class - MyListItem.
var itemsSource = this.MyGridView.ItemsSource;
this.MyGridView.ClearValue(RadGridView.ItemsSourceProperty);
this.MyGridView.ItemsSource = itemsSource;

Greetings,
Stefan Dobrev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Michael Fooks
Top achievements
Rank 1
answered on 06 Jan 2010, 01:11 AM
Hi,

That didn't seem to work for me. The supplied code doesn't work for me as the error occurs as soon as i change the datasource for the grid. So   I tried the following where as bound collection changes:

notificationGrid.ClearValue(

 

RadGridView.ItemsSourceProperty);

 

FilteredNofications = (

 

ListCollectionView)(CollectionViewSource.GetDefaultView(AllNotifications));

 

FilteredNofications.Filter =

 

delegate(object obj)

 

{
    // Do some filter logic

};

notificationGrid.ItemsSource = FilteredNofications;


When I assign the filter source I get the same error.  I tried with an inital collection containing a mixture of types and a collection containing a single type and then adding a different type to it and get the some errors.

Any indication of when an internal or a private build could be available to address this issue? 

0
Stefan Dobrev
Telerik team
answered on 06 Jan 2010, 07:09 AM
Hello Bronwen,

It seems that you are hitting some corner case with the filtered collection view you are using. Can you open a support ticket and attached a sample application that illustrates this problem exactly? This will help us debug this locally and provide you with a working solution.

Best wishes,
Stefan Dobrev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
GridView
Asked by
Michael Fooks
Top achievements
Rank 1
Answers by
Stefan Dobrev
Telerik team
Michael Fooks
Top achievements
Rank 1
Share this question
or