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

InvalidOperationException: Failed to compare two elements in the array.

8 Answers 1082 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jonathon
Top achievements
Rank 1
Jonathon asked on 06 Dec 2011, 10:02 PM
Hi,

I decided to make a new thread for this issue:
My GridView is a collection of IActivity which is an interface I made that 3 classes implement. This only happens when I try to adda new activity to the grid when it is sorted. When the Grid is not sorted, the activity is added to the grid. This is the error:

{System.InvalidOperationException: Failed to compare two elements in the array. ---> System.InvalidCastException: Unable to cast object of type 'FirstActivityViewModel' to type 'SecondActivityViewModel'.
   at Telerik.Windows.Data.FuncExtensions.<>c__DisplayClass1`2.<ToUntypedFunc>b__0(Object item)
   at Telerik.Windows.Data.FunctionComparer.Compare(Object x, Object y)
   at System.Collections.Generic.ArraySortHelper`1.InternalBinarySearch(T[] array, Int32 index, Int32 length, T value, IComparer`1 comparer)
   at System.Collections.Generic.ArraySortHelper`1.BinarySearch(T[] array, Int32 index, Int32 length, T value, IComparer`1 comparer)
   --- End of inner exception stack trace ---
   at System.Collections.Generic.ArraySortHelper`1.BinarySearch(T[] array, Int32 index, Int32 length, T value, IComparer`1 comparer)
   at System.Array.BinarySearch[T](T[] array, Int32 index, Int32 length, T value, IComparer`1 comparer)
   at System.Collections.Generic.List`1.BinarySearch(Int32 index, Int32 count, T item, IComparer`1 comparer)
   at System.Collections.Generic.List`1.BinarySearch(T item, IComparer`1 comparer)
   at Telerik.Windows.Data.KeyedCollection.IndexOf(Object value)
   at Telerik.Windows.Data.QueryableCollectionView.InternalIndexOf(Object item)
   at Telerik.Windows.Data.QueryableCollectionView.IndexOf(Object item)
   at Telerik.Windows.Data.DataItemCollection.IndexOf(Object value)
   at Telerik.Windows.Controls.DataControl.ItemCoerce(DependencyObject d, Object baseValue)
   at Telerik.Windows.PropertyMetadata.DoCoercion(Func`3 areValuesEqual, DependencyObject d, DependencyProperty dp, CoerceValueCallback coerceCallback, Object defaultBaseValue, Boolean forceNewBaseValue, Boolean& isReentrantImplicitCoercion)
   at Telerik.Windows.PropertyMetadata.PropertyChangeHook.OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue)
   at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
   at System.Windows.DependencyObject.RefreshExpression(DependencyProperty dp)
   at System.Windows.Data.BindingExpression.SendDataToTarget()
   at System.Windows.Data.BindingExpression.SourcePropertyChanged(PropertyPathListener sender, PropertyPathChangedEventArgs args)
   at System.Windows.PropertyPathListener.RaisePropertyPathStepChanged(PropertyPathStep source)
   at System.Windows.PropertyAccessPathStep.RaisePropertyPathStepChanged(PropertyListener source)
   at System.Windows.CLRPropertyListener.SourcePropertyChanged(Object sender, PropertyChangedEventArgs args)
   at System.Windows.Data.WeakPropertyChangedListener.PropertyChangedCallback(Object sender, PropertyChangedEventArgs args)
   at System.ComponentModel.PropertyChangedEventHandler.Invoke(Object sender, PropertyChangedEventArgs e)
   at GalaSoft.MvvmLight.ViewModelBase.RaisePropertyChanged(String propertyName)
   at Telerik.Windows.Controls.RadSplitButton.ExecuteCommand()
   at Telerik.Windows.Controls.RadSplitButton.OnClick()
   at Telerik.Windows.Controls.RadSplitButton.ActionButton_Click(Object sender, RoutedEventArgs e)
   at System.Windows.Controls.Primitives.ButtonBase.OnClick()
   at System.Windows.Controls.Button.OnClick()
   at Telerik.Windows.Controls.RadButton.OnClick()
   at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
   at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)}

Is there a fix for this?

Thanks,

Jonathon

8 Answers, 1 is accepted

Sort by
0
Christian Burkhardt
Top achievements
Rank 1
answered on 07 Dec 2011, 08:55 AM
I have exactly the same error. It wasn't there in the previous version of the gridview.
0
Christian Burkhardt
Top achievements
Rank 1
answered on 07 Dec 2011, 11:14 AM
I now compiled the sources and debugged it. The problem is in the FuncExtensions class, specifically this method:

        public static Func<object, object> ToUntypedFunc<T, TResult>(Func<T, TResult> func)
        {
            return item => func.Invoke((T) item);
        }

It tries to cast to T, but if you have items of multiple types in the gridview, it will be called once for the "wrong" type of object and try to cast it to T and fail.

Looking forward to a fix, as this actually broke my app :(
0
Vlad
Telerik team
answered on 07 Dec 2011, 12:07 PM
Hi,

 Will post more info about your scenario? What is your current grid version and what was the previous version?

Greetings,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Jonathon
Top achievements
Rank 1
answered on 08 Dec 2011, 11:15 PM
I'm currently using 2011.3.1116.1040. 
0
Luc
Top achievements
Rank 1
answered on 17 Jan 2012, 05:03 PM
Same error using  2011.3 1220 for SL5.. Any fix to this sortdescriptor issue
0
Vlad
Telerik team
answered on 17 Jan 2012, 05:05 PM
Hi,

 Please open support ticket and send us an example project where we can reproduce and debug the exception. 

Kind regards,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Josh Eastburn
Top achievements
Rank 1
answered on 15 Feb 2012, 04:47 PM
Has there been a resolution to this problem?
I am experiencing the same problem in my application running 2011.2.920.1040.

Thank you.
0
Josh Eastburn
Top achievements
Rank 1
answered on 15 Feb 2012, 05:09 PM
To be more precise about the error I am receiving, the exception is due to a NullReferenceException:

System.InvalidOperationException: Failed to compare two elements in the array. ---> System.NullReferenceException: Object reference not set to an instance of an object.

The exception is thrown when I try to sort on a column that sorts by a property of a related object that is NULL for an item.  However, I do not get the exception the first time when sorting by this column.  Only after repeatedly changing the sort order on other columns will the error occur.

The GridViewDataColumn with the problem:
<telerik:GridViewDataColumn IsReadOnly="True" DataMemberBinding="{Binding Vendoremployee.VendorEmployeeName}" SortMemberPath="Vendoremployee.FirstName" />

I looked at the data and the Vendoremployee property is null for two of the rows.  But why does this exception not occur the first time I sort on this column?
Is there a way to change the Sort behavior to trap this exception?
Tags
GridView
Asked by
Jonathon
Top achievements
Rank 1
Answers by
Christian Burkhardt
Top achievements
Rank 1
Vlad
Telerik team
Jonathon
Top achievements
Rank 1
Luc
Top achievements
Rank 1
Josh Eastburn
Top achievements
Rank 1
Share this question
or