Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move gridview selected row up/down on KeyUp or Keydown press

enter image description here

  1. The user selects one row
  2. there will be up arrow and down arrow.
  3. If the user wants to move up, the user clicks up arrow button
  4. If the user wants to move down, then the user clicks down arrow button
  5. if the row is at the top then up arrow button becomes disabled
  6. if the row is at the bottom then down arrow button becomes disabled

i tried this code but not at all working for the above scenario

private void key_up(object sender, EventArgs e)

{
    if (dataGridView1.CurrentRow == null) return;
    if (dataGridView1.CurrentRow.Index - 1 >= 0)
    {

        dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.CurrentRow.Index - 1].Cells[0];
        dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Selected = true;
    }
}
like image 466
sanjeev Avatar asked Nov 28 '22 02:11

sanjeev


1 Answers

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.

like image 114
Jegan Avatar answered Dec 05 '22 02:12

Jegan