TimeSort Error
Hi
I'm seeing a bug with Custom sort when doing a sort on DateTime. I have listed my custom sort code as well as the exception below. I believe this happens when updating a datetime of a record already in the list , a result of which causes the record to go to the top of the list. For example, if we have 10 records with timestamps from 9:00 to 9:10 each in a one minute increment and we update the 5th record from 9:05 to 9:30 , it might cause a problem. I could be wrong, but this is my observation so far.
Please let me know what you find.
Thanks.
internal class FillTimeCustomSort : ICustomSort
{
public int Compare(Row row1, Row row2, string fieldId, object value1, object value2, int defaultResult)
{
try
{
if (fieldId == "Time")
{
FillsModel tModel1 = row1.DataObject as FillsModel;
FillsModel tModel2 = row2.DataObject as FillsModel;
if (tModel1 != null && tModel2 != null)
{
DateTime t1 = ((FillsModel)row1.DataObject).TimeStamp;
DateTime t2 = ((FillsModel)row2.DataObject).TimeStamp;
if (t1 != null && t2 != null)
{
if (t1.Equals(t2))
return 0;
else
return t1.CompareTo(t2);
}
}
}
}
catch (Exception err)
{
Console.WriteLine("Error in TimeCustomSort " + err.ToString());
}
return defaultResult;
}
}
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.Collections.ArrayList.get_Item(Int32 index)
at Dapfor.Net.Internal.Helper.SingleNodeSort(ArrayList list, Int32 index, IComparer comparer)
at Dapfor.Net.Internal.NodeContainer.MoveNodeToUpdatedPosition(IList`1 sort, ICustomSort customSort, Node node, Int32 oldPosition)
at Dapfor.Net.Internal.Node.SafeUpdate()
at Dapfor.Net.Internal.Node.PropertyIdChangedTask.Execute(Node node, IDataField field, Boolean highlightRow)
at Dapfor.Net.Internal.Node.PropertyIdChangedTask.Execute()
at Dapfor.Net.Threading.GuiDispatcher.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
-
Hi,
The problem is in incorrect implementation of the Compare() method. Indeed, if the grid has sorted columns it stores rows in a sorted way. When you change the sort in a column or the grid gets a notification from INotifyPropertyChanged it calls a kind of the List.Sort (IComparer) method. Thus during value comparing they should be unchanged. When you compare TimeStamp values they can be changed and the grid will throw exceptions.
In order to avoid this problem, the grid does a snapshot of values by which it sorts. These values are passed as value1 and value2 to the Compare() method. So you should cast these values and compare them.
Best regards,
Dapfor
0 -
Thanks, that worked. I appreciate your quick response. I know I can be pain in asking too many questions at times but thanks for bearing with me.
0 -
Hi,
We are interested in quality of our products and answering your questions, we better understand our customers' needs.
Best regards,
Dapfor
0
Please sign in to leave a comment.
Comments
3 comments