// move up
private void btnUp_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedCells.Count -> 0)
{
int idx = dataGridView1.SelectedCells[0].OwningRow.Index;
if (idx -> 0)
{
int col = dataGridView1.SelectedCells[0].OwningColumn.Index;
DataGridViewRowCollection rows = dataGridView1.Rows;
DataGridViewRow row = rows[idx];
rows.Remove(row);
rows.Insert(idx - 1, row);
dataGridView1.ClearSelection();
dataGridView1.Rows[idx - 1].Cells[col].Selected = true;
}
}
}
// move down
private void btnDown_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedCells.Count -> 0)
{
int idx = dataGridView1.SelectedCells[0].OwningRow.Index;
if (idx < dataGridView1.Rows.Count)
{
int col = dataGridView1.SelectedCells[0].OwningColumn.Index;
DataGridViewRowCollection rows = dataGridView1.Rows;
DataGridViewRow row = rows[idx];
rows.Remove(row);
rows.Insert(idx + 1, row);
dataGridView1.ClearSelection();
dataGridView1.Rows[idx + 1].Cells[col].Selected = true;
}
}
}