I found one more problem with RemoveAt(int index) method.
If any ListChange event is generated during item deletion then by the time _list.RemoveAt(baseIndex) returns, DoSort() has been executed and _sortIndex list is up to date. Any further adjustment to this list make it invalid. When grid refreshes itself, exception occurs. In a hurry I fixed this problem by changing two functions:
public void RemoveAt(int index)
{
if (_sorted)
{
_initiatedLocally = true;
int baseIndex = OriginalIndex(index);
// remove the item from the source list
_list.RemoveAt(baseIndex);
//if equal, DoSort has happened in reponse to event fired in //_list.RemoveAt. Aadjust sorted index only if needed.
if (_list.Count != _sortIndex.Count)
{
// delete the corresponding value in the sort index
if (_sortOrder == ListSortDirection.Ascending)
_sortIndex.RemoveAt(index);
else
_sortIndex.RemoveAt(_sortIndex.Count - 1 - index);
// now fix up all index pointers in the sort index
foreach (ListItem item in _sortIndex)
if (item.BaseIndex > baseIndex)
item.BaseIndex -= 1;
}
OnListChanged(new ListChangedEventArgs(
ListChangedType.ItemDeleted, index));
_initiatedLocally = false;
}
else
_list.RemoveAt(index);
}
private void SourceChanged(
{
...
default:
// for anything other than add, delete or change
// just re-sort the list
if (!_initiatedLocally)
DoSort();
break;
}
From: oshokodk [mailto:cslanet@lhotka.net]
Sent: Thursday, December 07, 2006 4:27 PM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] SortedBindingList Delete BugI found one more problem with RemoveAt(int index) method.
If any ListChange event is generated during item deletion then by the time _list.RemoveAt(baseIndex) returns, DoSort() has been executed and _sortIndex list is up to date. Any further adjustment to this list make it invalid. When grid refreshes itself, exception occurs. In a hurry I fixed this problem by changing two functions:
public void RemoveAt(int index)
{
if (_sorted)
{
_initiatedLocally = true;
int baseIndex = OriginalIndex(index);
// remove the item from the source list
_list.RemoveAt(baseIndex);
//if equal, DoSort has happened in reponse to event fired in //_list.RemoveAt. Aadjust sorted index only if needed.
if (_list.Count != _sortIndex.Count)
{
// delete the corresponding value in the sort index
if (_sortOrder == ListSortDirection.Ascending)
_sortIndex.RemoveAt(index);
else
_sortIndex.RemoveAt(_sortIndex.Count - 1 - index);
// now fix up all index pointers in the sort index
foreach (ListItem item in _sortIndex)
if (item.BaseIndex > baseIndex)
item.BaseIndex -= 1;
}
OnListChanged(new ListChangedEventArgs(
ListChangedType.ItemDeleted, index));
_initiatedLocally = false;
}
else
_list.RemoveAt(index);
}
private void SourceChanged(
{
...
default:
// for anything other than add, delete or change
// just re-sort the list
if (!_initiatedLocally)
DoSort();
break;
}
Copyright (c) Marimer LLC