Change when moving from 2.0 to 2.0.1 - Swapping items in list - problems.

Change when moving from 2.0 to 2.0.1 - Swapping items in list - problems.

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


skagen00 posted on Monday, May 22, 2006

In BusinessListBase, this method is a new override it appears:

/// <summary>

/// Replaces the item at the specified index with

/// the specified item, first moving the original

/// item to the deleted list.

/// </summary>

/// <param name="index">The zero-based index of the item to replace.</param>

/// <param name="item">

/// The new value for the item at the specified index.

/// The value can be null for reference types.

/// </param>

/// <remarks></remarks>

protected override void SetItem(int index, C item)

{

RemoveItem(index);

base.SetItem(index, item);

}

This broke the following working code:

        swapItem = _editingQuery.Items[rowToSwap1];
        _editingQuery.Items[rowToSwap1] = _editingQuery.Items[rowToSwap2];
        _editingQuery.Items[rowToSwap2] = swapItem;

In one example, rowToSwap1 = 1 and rowToSwap2 = 0 in a 2 item list.

When the second line is executed (assigning rowToSwap1), it would remove item for index value 1 from the list and then try to assign to index value 1, which would now be invalid because it was removed. 

This sort of code is done to facilitate "move up" and "move down" in the collection.

I'm trying to figure out a nice solution to this, but in the meantime I wanted to post this to see if anyone handles swapping in a different manner or if there was a clever solution someone might have to offer. Once I figure something out (unless someone beats me to the punch) I'll post what I decided to do.

 

skagen00 replied on Monday, May 22, 2006

I suspect this would work for me. If someone who is more familiar with the framework might confirm this, I'd appreciate it. 

public void SwapPositions(int rowToSwap1, int rowToSwap2)

{

QueryItem swapItem1, swapItem2;

if (rowToSwap1 < 0 || rowToSwap1 >= this.Count ||

rowToSwap2 < 0 || rowToSwap2 >= this.Count)

{

throw new IndexOutOfRangeException("Valid range of collection is 0 to " +

(this.Count - 1).ToString() + ". Indexes provided were " +

rowToSwap1.ToString() + " and " + rowToSwap2.ToString() + ".");

}

swapItem1 = Items[rowToSwap1];

swapItem2 = Items[rowToSwap2];

this.RemoveAt(rowToSwap1);

this.Insert(rowToSwap1,swapItem2);

this.RemoveAt(rowToSwap2);

this.Insert(rowToSwap2, swapItem1);

this.DeletedList.Remove(swapItem1);

this.DeletedList.Remove(swapItem2);

}

RockfordLhotka replied on Tuesday, May 23, 2006

Not to be contrary, but SetItem() hasn't changed since 2.0 was officially released. My guess is that you haven't been using the RTM code, but rather have been using an earlier beta release.

SetItem() has to work the way it is in 2.0 RTM, because otherwise the concept of replacing an existing item with a new item wouldn't work properly - the old item would be simply discarded (the reference would be dropped) and it would never be deleted from the underlying database when Save() was called.

I think that your best bet for allowing reordering of items in the list is to take a copy of SortedBindingList and change it to provide a view that allows for arbitrary reordering of items.

mtagliaf replied on Monday, May 22, 2006

For ordered lists, I usually have an integer property that represents the sort order, and use this to control sorting on a grid (via a hidden column). Then the swap of two items is easy - simply exchange the value of this property.

matt tag

DavidDilworth replied on Tuesday, May 23, 2006

I agree with Matt, the use of a "display order array" or "sort order property" usually makes the problem of swapping and sorting much easier.

P.S.  Good to see you on the new forum Matt!

RockfordLhotka replied on Tuesday, May 23, 2006

Hi Matt!

Your idea of the extra property, coupled with SortedBindingList, seems like an excellent answer - very nice.

mtagliaf replied on Tuesday, May 23, 2006

It's good to feel wanted....

Now all I need is a week or two so I can effectively learn CSLA 2.0 and convert my apps....

matt tag

ajj3085 replied on Tuesday, May 23, 2006

Buy the book, and read it cover to cover.

I did this just before starting my first CSLA application, and it helped me emmencely.  I had all the ideas of the framework fresh in my mind, and I understood why I was writing certain code.

I soon got into a pattern, so alot of my business objects are structured the same way (one of the great benefits of using CSLA).

Andy

y0mbo replied on Wednesday, May 24, 2006

Have you found a soltution to this problem?

y0mbo replied on Wednesday, May 24, 2006

Nevermind. Dumb user error.

Copyright (c) Marimer LLC