Solved

Reorder Items in sql database

Posted on 2008-10-02
7
176 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

930 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now