WEBSWAPP Silverlight ASP.NET 3.5 ASP.NET 2.0 ASP.NET 1.0
Cascading Lists
ITemplate
DDL in GridView
G/V MultiSelect
Nested GridViews
RegExp
DataGrid Reorder
Events fire twice
Hyperlink Field
TextBox
UpdateParameters
MultiView
TreeView
Move rows Sort on Rank

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:

  1. 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.
  2. 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.
 Page Name
default.aspx
default.htm
default.asp
index.htm
index.aspx
index.asp
main.htm
main.aspx
main.asp

Source code:

Sort on Rank source code