• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 167
  • Last Modified:

Changeing the order of records in a bound datagrid using Up/Down buttons

My users would like to reorder the records in a bound datagrid by using Up/Down buttons.  The DG also manages additions/deletetions of records.  I have tried adding a field in the table to hold the SortOrderValue.  When moving a record up or down I can increase/decrease the SortOrderValue by 1 and rewrite the record to the database, but how do I change the SortOrderValue of the "other" record that is being reordered?   I guess what I'm asking is after updating the SortOrderValue for the selected record, how do I determine which other record will be affected so I can correct its SortOrderValue.

Thanks in advance,

1 Solution
Use a CurrencyManger to loop through all records next to the record you change and set previous order value + 1
Diego PazosCommented:
To move a record, you have to swap its ordinal with the one of the element in the position you want to move it to.
So to move record N one position upwards, you have to swap elements N and N-1, which is done by:

UPDATE table SET SortOrderValue = -1 WHERE SortOrderValue = N
UPDATE table SET SortOrderValue = N WHERE SortOrderValue = N - 1
UPDATE table SET SortOrderValue = N - 1 WHERE SortOrderValue = -1

To move it down, replace 'N - 1' with 'N + 1'.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now