Solved

Reorder Items in sql database

Posted on 2008-10-02
7
175 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
Comment Utility
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
Comment Utility
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
Comment Utility
Sounds like a great idea, I like it.  Would I somehow do that in the ItemInserting section??  
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 4

Expert Comment

by:GO-87
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you for the excellent help!!!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
This video discusses moving either the default database or any database to a new volume.

762 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

10 Experts available now in Live!

Get 1:1 Help Now