Problem with Custom Sorting
Hi,
I am trying to implement a CustomSort in my Grid. The grid uses a ThreadSafeBinding list and is updated realtime via INotifyPropertyChanged . I am seeing exceptions when the grid tries to sort using my custom sort. I have listed both my custom sort class as well as the exception I'm seeing. I have 4 columns in the grid. Can you tell me why I'm seeing this exception.
Thanks,
Deepak
class TimeCustomSort : ICustomSort
{
public int Compare(Row row1, Row row2, string fieldId, object value1, object value2, int defaultResult)
{
try
{
if (fieldId == "Time")
{
DateTime t1 = ((TickerModel)row1.DataObject).LastSaleTime;
DateTime t2 = ((TickerModel)row2.DataObject).LastSaleTime;
return t1 > t2 ? 1 : -1;
}
}
catch (Exception err)
{
Console.WriteLine("Error in TimeCustomSort " + err.ToString());
}
return defaultResult;
}
}
System.InvalidOperationException: Failed to compare two elements in the array. ---> System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: value
at Dapfor.Net.Internal.Helper.FindIndexInSortedArray(ArrayList list, Object value, IComparer comparer)
at Dapfor.Net.Internal.NodeContainer.Compare(Node x, Node y, IComparer comparer)
at Dapfor.Net.Internal.RowContainer.RowComparer.Compare(Object x, Object y)
at System.Array.BinarySearch(Array array, Int32 index, Int32 length, Object value, IComparer comparer)
--- End of inner exception stack trace ---
at System.Array.BinarySearch(Array array, Int32 index, Int32 length, Object value, IComparer comparer)
at Dapfor.Net.Internal.RowContainer.FindInsertionPosition(PrivateContext privateContext, Node node)
at Dapfor.Net.Internal.Node.Show()
at Dapfor.Net.Internal.Node.set_Filtered(Boolean value)
at Dapfor.Net.Internal.Node.SafeAddToChildren(Node node, Nullable`1 where)
at Dapfor.Net.Internal.Node.AddToChildrenTask.Execute(Node parent, IDataAccessor childAccessor, Nullable`1 where, Node& fakeNode)
at Dapfor.Net.Internal.Node.BindingListHandler.AddNewObject(Object value, Int32 index)
at Dapfor.Net.Internal.Node.BindingListHandler.OnSafeListChanged(Object sender, ListChangedEventArgs e)
at Dapfor.Net.Threading.GuiDispatcher.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
-
Hello,
We think that the problem is in the comparing method. You should return 0 if t1 == t2. Try to change the code and let us know if the problem is solved.
Best regards,
Dapfor
0 -
That didn't seem to help, I'm still getting the same error. This is the code I'm using
if (fieldId == "Time")
{
DateTime t1 = ((TickerModel)row1.DataObject).LastSaleTime;
DateTime t2 = ((TickerModel)row2.DataObject).LastSaleTime;if (t1 != null && t2 != null)
{
if (t1.Equals(t2))
return 0;
else
return t1.CompareTo(t2);
}
}0 -
Could you attach a small demo project demonstrating the problem? Which version of the grid do you use? Regards, Dapfor 0
Please sign in to leave a comment.
Comments
3 comments