Cleaned the code of Jegan and made it suitable for multiple datagridviews.
private static void MoveUp(DataGridView dgv)
{
if (dgv.RowCount <= 0)
return;
if (dgv.SelectedRows.Count <= 0)
return;
var index = dgv.SelectedCells[0].OwningRow.Index;
if (index == 0)
return;
var rows = dgv.Rows;
var prevRow = rows[index - 1];
rows.Remove(prevRow);
prevRow.Frozen = false;
rows.Insert(index, prevRow);
dgv.ClearSelection();
dgv.Rows[index - 1].Selected = true;
}
private static void MoveDown(DataGridView dgv)
{
if (dgv.RowCount <= 0)
return;
if (dgv.SelectedRows.Count <= 0)
return;
var rowCount = dgv.Rows.Count;
var index = dgv.SelectedCells[0].OwningRow.Index;
if (index == (rowCount - 2)) // include the header row
return;
var rows = dgv.Rows;
var nextRow = rows[index + 1];
rows.Remove(nextRow);
nextRow.Frozen = false;
rows.Insert(index, nextRow);
dgv.ClearSelection();
dgv.Rows[index + 1].Selected = true;
}