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

Unable to cast object of type 'TypeA' to type 'TypeB'

8 Answers 510 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Peter
Top achievements
Rank 1
Peter asked on 03 Jun 2011, 09:15 PM
TypeA & TypeB both implement the same base type.  We are binding the RadGridView's ItemSource to an IList<BaseType> baseList

When we have the grid view filtering out TypeA and re-generate our baseList object, the on property changed throws the following:

As a note, we are using a custom hierarchy (as found in the examples: Telerik\RadControls for WPF Q1 2011 SP1\Demos\Examples\GridView\Hierarchy\CustomHierarchy)
-Type B has a list of TypeA,
-only TypeB is expandable (to show list of TypeA).

System.InvalidCastException: Unable to cast object of type 'TypeA' to type 'TypeB'.
   at System.Linq.Enumerable.<CastIterator>d__aa`1.MoveNext()
   at Telerik.Windows.Data.QueryableCollectionView.PopulateInternalList(IQueryable view)
   at Telerik.Windows.Data.QueryableCollectionView.CreateInternalList()
   at Telerik.Windows.Data.QueryableCollectionView.EnsureInternalList()
   at Telerik.Windows.Data.QueryableCollectionView.get_InternalList()
   at Telerik.Windows.Data.QueryableCollectionView.InternalIndexOf(Object item)
   at Telerik.Windows.Data.QueryableCollectionView.TryRestorePreviousCurrency()
   at Telerik.Windows.Data.QueryableCollectionView.InitializeCurrencyOnRefresh()
   at Telerik.Windows.Data.QueryableCollectionView.RefreshOverride()
   at Telerik.Windows.Data.QueryableCollectionView.RefreshInternal()
   at Telerik.Windows.Data.QueryableCollectionView.RefreshOrDefer()
   at Telerik.Windows.Data.QueryableCollectionView.InvalidatePagingAndRefresh()
   at Telerik.Windows.Data.QueryableCollectionView.OnFilterDescriptorsCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
   at Telerik.Windows.Data.RadObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
   at Telerik.Windows.Data.ObservableItemCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.RemoveItem(Int32 index)
   at Telerik.Windows.Data.RadObservableCollection`1.RemoveItem(Int32 index)
   at System.Collections.ObjectModel.Collection`1.Remove(T item)
   at Telerik.Windows.Data.CollectionExtensions.RemoveItems[T](ICollection`1 collection, IEnumerable`1 items)
   at Telerik.Windows.Data.DataItemCollection.RemoveDescriptorsFromCollectionView()
   at Telerik.Windows.Data.DataItemCollection.set_CollectionView(QueryableCollectionView value)
   at Telerik.Windows.Data.DataItemCollection.CreateCollectionView(IEnumerable source)
   at Telerik.Windows.Data.DataItemCollection.SetItemsSource(IEnumerable source)
   at Telerik.Windows.Controls.GridView.GridViewDataControl.<>c__DisplayClass18.<Bind>b__17()
   at Telerik.Windows.Controls.CursorManager.PerformTimeConsumingOperation(FrameworkElement frameworkElement, Action action)
   at Telerik.Windows.Controls.GridView.GridViewDataControl.Bind(Object newValue)
   at Telerik.Windows.Controls.GridView.GridViewDataControl.OnItemsSourceChanged(Object oldValue, Object newValue)
   at Telerik.Windows.Controls.DataControl.OnItemsSourcePropertyChanged(DependencyObject origin, DependencyPropertyChangedEventArgs args)
   at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
   at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, OperationType operationType)
   at System.Windows.DependencyObject.InvalidateProperty(DependencyProperty dp)
   at System.Windows.Data.BindingExpression.Invalidate(Boolean isASubPropertyChange)
   at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
   at System.Windows.Data.BindingExpression.ScheduleTransfer(Boolean isASubPropertyChange)
   at MS.Internal.Data.ClrBindingWorker.NewValueAvailable(Boolean dependencySourcesChanged, Boolean initialValue, Boolean isASubPropertyChange)
   at MS.Internal.Data.PropertyPathWorker.UpdateSourceValueState(Int32 k, ICollectionView collectionView, Object newValue, Boolean isASubPropertyChange)
   at MS.Internal.Data.ClrBindingWorker.OnSourcePropertyChanged(Object o, String propName)
   at MS.Internal.Data.PropertyPathWorker.System.Windows.IWeakEventListener.ReceiveWeakEvent(Type managerType, Object sender, EventArgs e)
   at System.Windows.WeakEventManager.DeliverEventToList(Object sender, EventArgs args, ListenerList list)
   at System.ComponentModel.PropertyChangedEventManager.OnPropertyChanged(Object sender, PropertyChangedEventArgs args)
   at System.ComponentModel.PropertyChangedEventHandler.Invoke(Object sender, PropertyChangedEventArgs e)
   at OurCode.NotifyPropertyChanged[T](Expression`1 propertySelector) in [Our stack trace here...]

8 Answers, 1 is accepted

Sort by
0
Peter
Top achievements
Rank 1
answered on 03 Jun 2011, 09:26 PM
I also just got it to happen without filtering or sorting.  Just adding and deleting items (which causes the baseList to be manually re-generated in the back end)
0
Yavor Georgiev
Telerik team
answered on 08 Jun 2011, 08:36 AM
Hi Peter,

 Could you please try to set the RadGridView's ItemsSource like so:
radGridView1.ItemsSource = baseList.AsQueryable()

 Let me know if this helps.

Regards,
Yavor Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Peter
Top achievements
Rank 1
answered on 08 Jun 2011, 04:36 PM
The baseList.AsQueryable() does not work.  
When the baseList object is rebuilt in the back end,
the rad grid view does not update since it's bound to
the IQueriable object returned from the function.
0
Yavor Georgiev
Telerik team
answered on 08 Jun 2011, 04:41 PM
Hello Peter,

 How exactly do you rebuild the baseList object? Is it INotifyCollectionChanged, or do you call RadGridView.Rebind()?

Regards,
Yavor Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Peter
Top achievements
Rank 1
answered on 08 Jun 2011, 04:51 PM
When one of the lists that implements the base type is updaetd, we rebuild our baseList (IList) 
(so we ensure we have the correct heirarchy for the expandable items)

The baseList property does implement INotifyPropertyChanged, The list itself does also notify INotifyCollectionChanged.

*EDIT*
The stack trace provided starts at the notify property changed event.
0
Yavor Georgiev
Telerik team
answered on 09 Jun 2011, 12:22 PM
Hello Peter,

 I'm having a little trouble creating a project to reproduce the problem. As far as I understood, the baseList contains both TypeB and TypeA. Moreover, TypeB rows are expandable and contain a collection of TypeA objects. It would help if you can tell me how does the baseList generation works. I notice you reset the ItemsSource property of the RadGridView, but why do that if the object is INotifyCollectionChanged? Lastly, if you can tell me what what kinds of objects the baseList object contains when the exception occurs, it would help a lot to determine the problem.

Best wishes,
Yavor Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Peter
Top achievements
Rank 1
answered on 14 Jun 2011, 05:34 PM

Our BaseList is re-generated on property changed of a handful of other items.  TypeA - TypeF all implement the same BaseType.  TypeB also contains a list of the BaseType (which can contain a list of TypeA & TypeC-TypeF).  BaseList contains a list of BaseType (which can contain TypeA - TypeF).  We have a conditional hierarchy in place to make it so that only objects of TypeB are expandable, which shows it's list of TypeA & TypeC-TypeF.  

Our top level object has an independent list for TypeA, TypeB, TypeC, TypeD, TypeE, & TypeF (as that is how it is actually stored in the database).  BaseList is generated any time any of these lists are added to or removed from.

At the end of the populate routine the following is run.  (The BaseList property calls on property changed which is where the error occurs)
this.BaseList =	(from BaseType b in baseList orderby b.Time descending select b).ToList();

We have logic in place to build the BaseList (calulated from our top level object's BaseList) in such a way that if the object (TypeB-TypeF) is contained by a parent object (TypeA) it will not be added to the list independently.

The issue comes in when we would have our objects set up as follows.  We have 1 TypeB and 2 TypeA.  one of the TypeA objects is a child of the Type A object
-TypeB(1)
   -TypeA(2)
-TypeA(3)

We then do a top level filter (with the built in grid filter) to not show TypeA we see the following
-TypeB(1)
   -TypeA(2)

Then in the meantime another action deletes TypeA(3) which re-builds the BaseList.  After this is where we get the conversion error. Whenever we start trying to add another object back in (such as TypeB with a child of TypeA)


I hope this makes sense, I tried to keep the types generic, but I know that can also make it harder to read.  If you would want to see the actual code we're using i'd be comfortable sending it to you, just not posting it publicly.

Thanks again for your help.
0
Yavor Georgiev
Telerik team
answered on 15 Jun 2011, 08:40 AM
Hi Peter,

 You can open a separate support ticket, which will be viewable only by you and the assigned support officer. You can then attach your application code so that I can run it locally and try to reproduce the exception in the RadGridView code. I think that this is the best option, as I'm afraid I won't be able to accurately replicate your scenario on my own.

Greetings,
Yavor Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Peter
Top achievements
Rank 1
Answers by
Peter
Top achievements
Rank 1
Yavor Georgiev
Telerik team
Share this question
or