the way to do is ,
On key_up or button up clicked
1) Get the current selected row index
2) Set the current selected row to (index + 1) as long as the index +1 is less than the row count.
Do the negation for the key_Down or button down clicked.
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.Equals(Keys.Up))
{
moveUp();
}
if (e.KeyCode.Equals(Keys.Down))
{
moveDown();
}
e.Handled = true;
}
private void moveUp()
{
if (dataGridView1.RowCount > 0)
{
if (dataGridView1.SelectedRows.Count > 0)
{
int rowCount = dataGridView1.Rows.Count;
int index = dataGridView1.SelectedCells[0].OwningRow.Index;
if (index == 0)
{
return;
}
DataGridViewRowCollection rows = dataGridView1.Rows;
// remove the previous row and add it behind the selected row.
DataGridViewRow prevRow = rows[index - 1];
rows.Remove(prevRow);
prevRow.Frozen = false;
rows.Insert(index, prevRow);
dataGridView1.ClearSelection();
dataGridView1.Rows[index - 1].Selected = true;
}
}
}
private void moveDown()
{
if (dataGridView1.RowCount > 0)
{
if (dataGridView1.SelectedRows.Count > 0)
{
int rowCount = dataGridView1.Rows.Count;
int index = dataGridView1.SelectedCells[0].OwningRow.Index;
if (index == (rowCount - 2)) // include the header row
{
return;
}
DataGridViewRowCollection rows = dataGridView1.Rows;
// remove the next row and add it in front of the selected row.
DataGridViewRow nextRow = rows[index + 1];
rows.Remove(nextRow);
nextRow.Frozen = false;
rows.Insert(index, nextRow);
dataGridView1.ClearSelection();
dataGridView1.Rows[index + 1].Selected = true;
}
}
}
you can see I have separated the move up and down methods, so if you want to use the button clicked events instead of key up and key down event, you can call them when needed.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…