DataGrid Rows Reordering
Unlike sorting the DataGrid based on an index field, re-ordering the rows, based
on explicit user requests (to move a row up or down), is not as simple.
The approach that one takes is very much dependent on the type of data structure
one has bound to the datagrid. I can offer here 2 approaches:
- sort based on a rank field. Then upon moving up or down swap the value of that field
with the previous or next record respectively.
- locate the record that is to be moved using the DataGrid.DataKeys collection while
processing the DataGrid.ItemCommand event. I make a copy of that DataTableRow then
delete the original one, acceptChanges on the DataTable, then re-insert it at the
desired position (whether moving up or down). This approach can pose concurrency
issues upon saving the table to the database because of the primary keys sequence.