Hi Rocky, In SortedBindingList (in the last version 3.8.3), there is : private int SortedIndex(int originalIndex) { int result = 0; if (_sorted) { for (int index = 0; index < _sortIndex.Count; index++) { if (_sortIndex[index].BaseIndex == originalIndex) { result = index; break; } } if (_sortOrder == ListSortDirection.Descending) result = _sortIndex.Count - 1 - result; } else result = originalIndex; return result; } Or in the VB version (it is the same but I prefer) : Private Function SortedIndex(ByVal originalIndex As Integer) As Integer Dim result As Integer If _sorted Then For index As Integer = 0 To _sortIndex.Count - 1 If _sortIndex(index).BaseIndex = originalIndex Then result = index Exit For End If Next If _sortOrder = ListSortDirection.Descending Then result = _sortIndex.Count - 1 - result End If Else result = originalIndex End If Return result End Function Everything is good if originalIndex exist in _sortIndex. But what happen if it is not the case : the function returns either 0 or _sortIndex.Count – 1. It is not good so I add : If _sorted Then For index As Integer = 0 To _sortIndex.Count - 1 If _sortIndex(index).BaseIndex = originalIndex Then result = index Exit For End If Next If _sortOrder = ListSortDirection.Descending Then … I use -1 because you use -1 in : public int Find(PropertyDescriptor property, object key) { if (_supportsBinding) return SortedIndex(_bindingList.Find(property, key)); else return -1; } I think that it is a bug. Dominique Dim result As Integer = -1
If result = -1 then Return result
Can that happen thought? Is there a scenario where the index of an item needs to be translated into the indexed position, and where the item is somehow in the list, but not in the sorted index?
If that can happen the bug isn't in the SortedIndex() method, it is in whatever code allowed an item to get into the list, and not into the sorted index.
I came in this situation looking a key using :
public int Find(PropertyDescriptor property, object key)
{
if (_supportsBinding)
return SortedIndex(_bindingList.Find(property, key));
else
return -1;
}
The key doesn't exist then _bindingList.Find(property, key) return -1 but SortedIndex(_bindingList.Find(property, key)) return 0.
Then for my code, the key exist at index 0 and it is not true.
OK, that makes sense. I'll add this to the bug list.
Hi,
I'd rather change the code in Find to
public int Find(PropertyDescriptor property, object key)
{
if (_supportsBinding)
{
int originalIndex = _bindingList.Find(property, key);
return (originalIndex > -1) ? SortedIndex(originalIndex) : -1;
}
else
return -1;
}
as OriginalIndex is also used for sending events to the UI that should never get -1 as index and may introduce new bugs.
Hi Dominique,
I have updated the SortedBindingList in $/core/branches/V3-8-x (and 4.0) with changes to Find and better databinding behavior.
If you have time please download changes and verify that it solves your issues.
Copyright (c) Marimer LLC