BUG: SortedBindingList CSLA 3.6.x and older

BUG: SortedBindingList CSLA 3.6.x and older

Old forum URL: forums.lhotka.net/forums/t/7193.aspx


JonnyBee posted on Tuesday, June 30, 2009

Hi,

Just dumped into a bug in SortedBindingList when adding new item to an unsorted wrapped list.  This causes the ItemAdded event to be raised 2 times and confuses the datagridview to show 2 new rows in list even if only one item is added to list.

/jonnybee

    private void SourceChanged(
      object sender, ListChangedEventArgs e)
    {
      if (_sorted)
      {
        switch (e.ListChangedType)
        {
          case ListChangedType.ItemAdded:
            T newItem = _list[e.NewIndex];
            if (e.NewIndex == _list.Count - 1)
            {
              object newKey;
              if (_sortBy != null)
                newKey = _sortBy.GetValue(newItem);
              else
                newKey = newItem;

              if (_sortOrder == ListSortDirection.Ascending)
                _sortIndex.Add(
                  new ListItem(newKey, e.NewIndex));
              else
                _sortIndex.Insert(0,
                  new ListItem(newKey, e.NewIndex));
              if (!_initiatedLocally)
                  OnListChanged(
                    new ListChangedEventArgs(
                    ListChangedType.ItemAdded,
                    SortedIndex(e.NewIndex)));
            }
            else
              DoSort();
            break;

          case ListChangedType.ItemChanged:
            // an item changed - just relay the event with
            // a translated index value
            OnListChanged(
              new ListChangedEventArgs(
              ListChangedType.ItemChanged, SortedIndex(e.NewIndex), e.PropertyDescriptor));
            break;

          case ListChangedType.ItemDeleted:
            if (!_initiatedLocally)
              DoSort();
            break;

          default:
            // for anything other than add, delete or change
            // just re-sort the list
            if (!_initiatedLocally)
              DoSort();
            break;
        }
      }
      else
          // must also check for _initiatedLocally when list is unsorted
          if (!_initiatedLocally)

            OnListChanged(e);
    }

ajj3085 replied on Tuesday, June 30, 2009

Well, you're supposed to be adding to the SortedBindingList, as it's a live updatable view into the unsorted collection.

Actually, you should probably be calling Add on the BindingSource component which is handling the list... if it is not "in the loop," things go bad very quickly.

RockfordLhotka replied on Tuesday, June 30, 2009

Added to bug list

http://www.lhotka.net/cslabugs/edit_bug.aspx?id=436

Copyright (c) Marimer LLC