Ordered List

Ordered List

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


josh.kodroff posted on Wednesday, May 30, 2007

I have a (CSLA) list where order matters: each item in the list has a sequence number, which can be set by the user.

I followed the implementation in this thread: http://forums.lhotka.net/forums/thread/7131.aspx

So I have code that looks like this (this is the only code I wrote for this implementation):
   Public Sub MoveUp(ByVal index As Integer)
        If index > 0 And Me.Count > 1 Then
            RaiseListChangedEvents = False

            Dim temp As StudyComponent = Me(index)
            Me(index) = Me(index - 1)

            Me(index - 1) = temp

            ' Seq No starts at 1 so we need to add 1 to the index.
            Me(index).SetSeqNo(index + 1)
            Me(index - 1).SetSeqNo(index)
            RaiseListChangedEvents = True
        End If
    End Sub

    Public Sub MoveDown(ByVal index As Integer)
        If index < Me.Count - 1 And Me.Count > 1 Then
            RaiseListChangedEvents = False
            Dim temp As StudyComponent = Me(index)
            Me(index) = Me(index + 1)
            Me(index + 1) = temp

            ' Seq No starts at 1 so we need to add 1 to the index.
            Me(index).SetSeqNo(index + 1)
            Me(index + 1).SetSeqNo(index + 2)
            RaiseListChangedEvents = True

        End If
    End Sub

The problem is, whenever my MoveUp or MoveDown is called, items are added to the DeletedList collection, which in turn is calling MarkNew.  This means that whenever I change the order of my list, the Insert function is called instead of the Update function.

Can anyone lend any help?

Thanks.
JK

SimpleCSLA replied on Wednesday, May 30, 2007

Any reason why you couldn't put a button on the page that has the Update code in it?

That way you wouldn't make any calls to the database until the user was done changing the indexes.

josh.kodroff replied on Wednesday, May 30, 2007

I'm not sure what you mean.  I'm not making any calls to the database except through the standard data acess methods in my business objects.

RockfordLhotka replied on Wednesday, May 30, 2007

Can you just wrap your collection in a SortedBindingList and sort on your sequence property value?

In the database you can, of course, store the sequence value as well, but this way you don't really care about the order of items in memory, yet they would show up in order on the screen because they'd be sorted.

josh.kodroff replied on Wednesday, May 30, 2007

Rocky, you seem to be implying that if I just change the seq no property values instead of swapping members of the list, this will solve my problem.

Is this correct?

Also, while it's not critical that I understand why this would solve my problem (so long as it does), why are the items being added to the DeletedList when I'm swapping them?

Also also, consider this my request for an ordered list class in the next CSLA.NET version.  ;)

RockfordLhotka replied on Wednesday, May 30, 2007

Yes, you can change the seq # property and then re-apply the sort and the items will display in order based on that property.

 

If you look at your MoveUp/MoveDown code you’ll see that you are doing a “set” operation on two items in the list. A SetItem operation means that a new object is replacing an existing object in the list. For n-level undo to work, BusinessListBase must be able to undo that operation – eliminating the newly added item and restoring the original. To accomplish this, the original (which is obviously no longer active in the real list) must be moved to the deleted list, and the newly added item must have its editleveladded value set.

 

In CSLA 3 I address a bit of an oversight here, by also ensuring that the newly added item is at the same edit level as the collection itself – thus addressing a bug that would appear in some scenarios when doing in-place editing of a list in a grid control.

 

Regarding an ordered list: CSLA 1.x collections had a native ability to sort themselves. That was a serious PITA, and turns out to have been a poor implementation choice. The CSLA 2 approach of using SortedBindingList to create a sorted view of a list is a far superior solution – both in terms of implementation simplicity, but also in terms of composability – for instance, being able to combine SortedBindingList and FilteredBindingList to get a sorted, filtered, view.

 

Rocky

 

Copyright (c) Marimer LLC