Solved

Reorder Items in sql database

Posted on 2008-10-02
7
188 Views
Last Modified: 2013-11-27
Hi all, looking for some help on re-ordering.  I have a parent with several siblings, the siblings are ordered by a display_order field.  I need to be able to drop a NEW sibling in the middle and re-order the siblings accordingly...  I am thinking about making the display_order field go by 10's or 100's, and drop a new one in the middle, but I really don't like this solution and it would eventually fill up anyway.  I'm sure there is some GREAT code out there to help out with this, or at least some ideas on how to go about it.  I am doing this in Visual Studio 2005 VB.net.  They are adding the siblings in via a detailsview grid.

Thanks,
Randy
0
Comment
Question by:ratkinso666
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 3

Accepted Solution

by:
chrios earned 500 total points
ID: 22623902
Not really sql or VB specific, but this is how I would have done it.

Assuming you start with five siblings ordered 1,2,3,4,5

If you want to insert a new one after 2, just increase the number for all siblings above 2 by one, starting with the highest number (5) and going down to 3 (2+1).
5->6
4->5
3->4
The new ordering numbers are now 1,2,4,5,6
Add the new sibling with number 3 and you're done.


0
 
LVL 4

Expert Comment

by:GO-87
ID: 22623989
Instead of trying to manually insert numbers between existing numbers, which will eventually require renumbering (even if you were to use decimals, you'd eventually run out of numbers), why not implement the list in a simple Collection?
This would still require that you are able to identify the next (or previous) sibling when you're about to insert the new sibling
For example:
Private colSiblings As New Collection
Then you can insert a new sibling relative to its adjacent sibling, for example:
colSiblings.Add(argNewSibling,  , argBeforeSibling, )
or
colSiblings.Add(argNewSibling,  ,  , argAfterSibling)
0
 

Author Comment

by:ratkinso666
ID: 22624040
Sounds like a great idea, I like it.  Would I somehow do that in the ItemInserting section??  
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 4

Expert Comment

by:GO-87
ID: 22624409
Yes,
argNewSibling would be an object (or a number, or a string, ...) representing the new sibling; and argBeforeSibling (or argAfterSibling) would be the desired neighbour for the new sibling. Remember, if you've inserted the items as numbers, then you need to refer to numbers when you compare (and likewise, if you inserted strings, you need to compare against strings).
I hope that makes sense.
0
 

Author Comment

by:ratkinso666
ID: 22625112
coming along nicely, but what is the syntax to pass more than one value from the gridview to the detailsview??  I have two parameters (in the datakey names) in the gridview BA_ID, BA_DispOrder.  I need the BA_ID to pass to my next gridview and since it is my primary I have no problem using it, but I need the BA_DispOrder to know my selection from the gridview, after I get this last part I will be finished with this.  This is what I am trying to use in my Insert Parameters section:
<asp:ControlParameter ControlID="GridView2" Name="BA_DispOrder" PropertyName="SelectedValue"
                                                Type="Int32" />
I need it as my value, this works for the first parameter in a list, but I guess not for the secondary one??
Thanks,
Randy
0
 

Author Comment

by:ratkinso666
ID: 22625138
I know in the code behind I would get it like this:

        Dim NewOrder As String = (GridView2.SelectedDataKey.Values("BA_DispOrder").ToString())

so, I could run an update on this in the code behind, after I do my insert, but I would rather do it on the insert from the detailsview...
0
 

Author Closing Comment

by:ratkinso666
ID: 31502362
Thank you for the excellent help!!!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question