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.
In this approach I use a Rank field to resort the datatable before binding to the
DataGrid. This approach should avoid any concurrency issue upon saving that might
result from removing and deleting rows that have primary keys on the database.
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.