1) 如果dgOutImage.DataSource=集合(RecFileArgs)

(向上移动)

dgOutImage.DataSource = new BindingList<RecFileArgs>(RecFileArgs1);

//如果选择的是多行

if (dgOutImage.SelectedRows.Count > 1)

{

return;

}

dgOutImage.Focus();

//目前选择的行号

int rowIndex = dgOutImage.CurrentRow.Index;

if (rowIndex != 0)

{

RecFileArgs _reccCurr = dvrPlayListRight[rowIndex];

RecFileArgs _reccPer = dvrPlayListRight[rowIndex - 1];

dvrPlayListRight[rowIndex] = _reccPer;

dvrPlayListRight[rowIndex - 1] = _reccCurr;

dgOutImage.DataSource = dvrPlayListRight;

dgOutImage.Refresh();

dgOutImage.Rows[rowIndex - 1].Selected = true;

dgOutImage.CurrentCell = dgOutImage.Rows[rowIndex - 1].Cells[0];

}

if (rowIndex - 1 == 0)

{

向上按钮.Enabled = false;

}

else

{

向上按钮.Enabled = true;

}

2) 如果dgOutImage.DataSource= DataTable

(向下移动)

object[] _rowData = DataTable.Rows[i].ItemArray;

DataTable.Rows[i].ItemArray = DataTable.Rows[i+1].ItemArray

DataTable.Rows[i+1].ItemArray = _rowData;

===============================================

数据库表中设置一个排序字段 SortOrder int

SQL code
INSERT tb(Title,SortOrder) SELECT @Title,(SELECT ISNULL(MAX(SortOrder),0)+1 FROM tb) AS SortOrder Create Procedure Tb_Move ( @Id int, @UpOrDown nchar(4) ) AS declare @CurrentOrder int , @ExOrder int SELECT @CurrentOrder=SortOrder FROM tb WHERE Id=@Id IF @UpOrDown='up' SELECT TOP 1 @ExOrder=SortOrder FROM tb WHERE SortOrder<@CurrentOrder ORDER BY SortOrder DESC IF @UpOrDown='down' SELECT TOP 1 @ExOrder=SortOrder FROM tb WHERE SortOrder>@CurrentOrder ORDER BY SortOrder ASC IF @ExOrder IS NULL RETURN ELSE BEGIN UPDATE tb SET SortOrder=0 WHERE SortOrder=@CurrentOrder UPDATE tb SET SortOrder=@CurrentOrder WHERE SortOrder=@ExOrder UPDATE tb SET SortOrder=@ExOrder WHERE SortOrder=0 END GO



C# code
public void UpOrDown(int Id, string upORdown) { try { SqlParameter[] sqlParams = new SqlParameter[] { new SqlParameter("@Id",Id), new SqlParameter("@UpOrDown",upORdown) }; DBHelper.ExecuteNonQuery("Tb_Move", sqlParams); } catch { } } protected void GridView_RomCommand(object sender,GridViewRowEventArgs e) { swith(e.CommandName) { case "up": //注意前台这里要给向上移动的按钮加CommandName="up"属性,且CommandArgument='<%#Eval("Id") %>' UpOrDown(int.Parse(e.CommandArgument.ToString()),"up"); BindGridView(); break; case "down": //注意前台这里要给向上移动的按钮加CommandName="down"属性,且CommandArgument='<%#Eval("Id") %>' UpOrDown(int.Parse(e.CommandArgument.ToString()),"down"); BindGridView(); break; } } private void BindGridView() { DataSet ds=DBHelper.ExcuteNoQuery("SELECT * FROM tb ORDER BY SortOrder ASC"); //按顺序ASC取.且绑定、 }