4
votes

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;
    }
}
7

7 Answers

18
votes

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.

5
votes

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;
    }
1
votes

For me this worked:

 public 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;
    }

    public 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 - 1) // Here used 1 instead of 2
            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;
    }

The difference to Mario's code is that I used (rowCount - 1) instead of (rowCount - 2) ... After changing this, it worked perfectly. Before the move-down didn't work if you only have 2 rows in your DataGridView...

0
votes

If you want to move the selected row up/down as many times as you want, you can use this code to move:

Up:

if (dataGridView1.SelectedRows[0].Index != 0) {
  for (int j = 0; j < this.dataGridView1.Columns.Count; j++) {
    object tmp = this.dataGridView1[j, dataGridView1.SelectedRows[0].Index].Value;
    this.dataGridView1[j, dataGridView1.SelectedRows[0].Index ].Value = this.dataGridView1[j, dataGridView1.SelectedRows[0].Index - 1].Value;
    this.dataGridView1[j, dataGridView1.SelectedRows[0].Index - 1].Value = tmp;
  }
  int a = dataGridView1.SelectedRows[0].Index;
  dataGridView1.ClearSelection();
  this.dataGridView1.Rows[a - 1].Selected = true;
}

Down:

 if (dataGridView1.SelectedRows[0].Index != dataGridView1.Rows.Count - 2) {
   for (int j = 0; j < this.dataGridView1.Columns.Count; j++) {
     object tmp = this.dataGridView1[j, dataGridView1.SelectedRows[0].Index].Value;
     this.dataGridView1[j, dataGridView1.SelectedRows[0].Index].Value = this.dataGridView1[j, dataGridView1.SelectedRows[0].Index + 1].Value;
     this.dataGridView1[j, dataGridView1.SelectedRows[0].Index + 1].Value = tmp;
   }
   int i = dataGridView1.SelectedRows[0].Index;
   dataGridView1.ClearSelection();
   this.dataGridView1.Rows[i + 1].Selected = true;
 }
0
votes

Here is a very small solution for that issue:

    private void DataGridView_KeyDown(object sender, KeyEventArgs e)
    {
        //I use only one function for moving with the information
        //e.KeyCode == Keys.Up = move up, else move down
        if (e.KeyCode.Equals(Keys.Up) || e.KeyCode.Equals(Keys.Down))
        {
            MoveUpDown(e.KeyCode == Keys.Up);
        }
        e.Handled = true;
    }

    private void MoveUpDown(bool goUp)
    {
        try
        {
            int currentRowindex = DataGridView.SelectedCells[0].OwningRow.Index;

            //Here I decide to change the row with the parameter
            //True -1 or False +1
            int newRowIndex = currentRowindex + (goUp ? -1 : 1);

            //Here it must be ensured that we remain within the index of the DGV
            if (newRowIndex > -1 && newRowIndex < DataGridView.Rows.Count)
            {
                DataGridView.ClearSelection();
                DataGridView.Rows[newRowIndex].Selected = true;
            }
        }
        catch (Exception)
        {
            MessageBox.Show("Error");
        }

    }

Sorry, I thought my code was self-explanatory. I hope I made with the comments clear how I proceeded with that issue

0
votes

Resumindo mais:

private void MoveUpDown(bool goUp)
    {
        int newRowIndex = DataGridView.SelectedCells[0].OwningRow.Index + (goUp ? -1 : 1);
        if (newRowIndex > -1 && newRowIndex < DataGridView.Rows.Count)
        {
            DataGridView.ClearSelection();
            DataGridView.Rows[newRowIndex].Selected = true;
        }
    }
0
votes
    dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;//FullRowSelect
     int selectedrowindexindgv2 =-1;
      private void moveUp()
            {
                if (dataGridView2.RowCount <= 0)
                    return;
    
                if (dataGridView2.SelectedRows.Count <= 0)
                    return;
    
                if (selectedrowindexindgv2 <= 0)
                    return;
    
                DataGridViewRowCollection rows = dataGridView2.Rows;
    
                // remove the previous row and add it behind the selected row.
                DataGridViewRow prevRow = rows[selectedrowindexindgv2 - 1];
                rows.Remove(prevRow);
                prevRow.Frozen = false;
                rows.Insert(selectedrowindexindgv2, prevRow);
                dataGridView2.ClearSelection();
                dataGridView2.Rows[selectedrowindexindgv2 - 1].Selected = true;
                selectedrowindexindgv2 -= 1;
                return;
            }
    
            private void moveDown()
            {
                try
                {
                    if (dataGridView2.RowCount <= 0)
                        return;
    
                    if (dataGridView2.SelectedRows.Count <= 0)
                        return;             
    
                    if (selectedrowindexindgv2 == dataGridView2.Rows.Count - 1) // Here used 1 instead of 2
                        return;
    
                    DataGridViewRowCollection rows = dataGridView2.Rows;
                    // remove the next row and add it in front of the selected row.
                    DataGridViewRow nextRow = rows[selectedrowindexindgv2 + 1];
                    rows.Remove(nextRow);
                    nextRow.Frozen = false;
                    rows.Insert(selectedrowindexindgv2, nextRow);
                    dataGridView2.ClearSelection();
                    dataGridView2.Rows[selectedrowindexindgv2 + 1].Selected = true;
                    selectedrowindexindgv2 += 1;
                }catch(Exception) { }
    
            }
    
       private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                if (dataGridView2.Rows.Count > 0 && e.RowIndex >= 0 && e.ColumnIndex >= 0)
                {
                    try
                    {
                        selectedrowindexindgv2 = e.RowIndex;
    }
  private void pictureBox6Up_Click(object sender, EventArgs e)
        {
            moveUp();
        }

        private void pictureBox7Down_Click(object sender, EventArgs e)
        {
            moveDown();
        }