Re: Swap nodes in a linked list

Ryan Heath <[email protected]>
Newsgroups gmane.comp.windows.devel.dotnet.clr
Message-ID <[email protected]>
ElementAt returns you T that is BandLine,
but your really want a LinkedListNode<BandLine>

You could create your own ElementAt which returns a LinkedListNode,
its very straight forward.
Move from list.First to the Next until you arrive at the wanted "index".

When you have the LinkedListNode, the remaining code is straight forward as well

LinkedListNode<BandLine> lowerBand =
list.ElementAt<BandLine>(dgvBands.SelectedRows[0].Index); // NOTE this
is your ElementAt extension which returns a LinkedListNode<T>
LinkedListNode<BandLine> upperBand = lowerBand.Previous;

currentBand.Remove(lowerBand);
currentBand.AddBefore(upperBand, lowerBand);

// Ryan

On Jan 22, 2008 9:29 AM, Brady Kelly <[email protected]> wrote:
> Sorry if this is very obvious, but it escapes me now at a time of impending
> review.  I have a LinkedList<BandLine>, representing a report Band, where
> BandLine is one line of text in the band, e.g. one of two detail line types,
> or a header line.
>
>
>
> I present my linked list using a DataGridView, and am trying to implement
> 'Move Up' and 'Move Down' commands for band lines, which involves swapping
> nodes in my linked list.  Now getting the BandLine to move is easy, using
> ElementAt and an index provided by the grid, but to complete the swap with
> AddBefore, I need a LinkedListNode at a specific index.  How can I get this?
> My code is as follows, and AddBefore is incomplete without the node I
> mention:
>
>
>
> private void MoveBandUp()
>
>
>
>                BandLine lowerBand =
> currentBand.ElementAt<BandLine>(dgvBands.SelectedRows[0].Index);
>
>                BandLine upperBand =
> currentBand.ElementAt<BandLine>(dgvBands.SelectedRows[0].Index - 1);
>
>                currentBand.Remove(lowerBand);
>
>                currentBand.AddBefore(???, lowerBand)
>
>
> ===================================
> This list is hosted by DevelopMentor(R)  http://www.develop.com
>
> View archives and manage your subscription(s) at http://discuss.develop.com
>

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.