Move an unbound DataGridView row up or down via code (VB.NET)

Doing something as simple as moving an unbound DataGridView (dgv) row up or down via code really shouldn’t be as difficult of a task as Visual Studio makes it.

I started out by creating two buttons on a Windows Form called btn_Up and btn_Down, respectively.  I then created a single sub routine that would be passed in the number of rows (+1 or -1) that the current row should be moved.  For example, to move a row “up” in dgv, it’s row index will be one less than it currently is (before the move)

Private Sub btn_Up_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Up.Click

        ‘If already on the first row, don’t try to move the row up

        If Me.dgv_FinishingScheduleColumns.CurrentCell.RowIndex = 0 Then

            Exit Sub

        End If

MoveRow(-1) ‘ move up in the datagridview (row index is 1 less)

End Sub

Private Sub btn_Down_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Down.Click

‘If already on the bottom row, don’t try to move the row down

        If Me.dgv_FinishingScheduleColumns.CurrentCell.RowIndex = Me.dgv_FinishingScheduleColumns.RowCount – 1 Then

            Exit Sub

End If

MoveRow(1) ‘ move down in the datagridview (row index is 1 more)

End Sub

Private Sub MoveRow(ByVal i As Integer)

Try

If (Me.dgv.SelectedCells.Count > 0) Then

Dim curr_index As Integer = Me.dgv.CurrentCell.RowIndex

Dim curr_col_index As Integer = Me.dgv.CurrentCell.ColumnIndex

Dim curr_row As DataGridViewRow = Me.dgv.CurrentRow

Me.dgv.Rows.Remove(curr_row)

Me.dgv.Rows.Insert(curr_index + i, curr_row)

Me.dgv.CurrentCell = Me.dgv(curr_col_index , curr_index + i)

End If

Catch ex As Exception

‘ do nothing if error encountered while trying to move the row up or down

End Try

End Sub

In addition to moving the row that contains the selected cell up or down, the MoveRow sub routine also selects the related cell in its “new” location after it has been moved.  I kept trying to set the CurrentCell.Selected value, but in the end, I needed to actually specify what the CurrentCell actually was.

Happy programming!

Private Sub btn_Up_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Up.Click
MoveRow(-1) ‘ move up in the datagridview (row index is 1 less)
End Sub
Private Sub btn_Down_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Down.Click
MoveRow(1) ‘ move down in the datagridview (row index is 1 more)
End Sub
Private Sub MoveRow(ByVal i As Integer)
Try
If (Me.dg_Environments.SelectedCells.Count > 0) Then
Dim curr_index As Integer = Me.dg_Environments.CurrentCell.RowIndex
Dim curr_cell_index As Integer = Me.dg_Environments.CurrentCell.ColumnIndex
Dim curr_row As DataGridViewRow = Me.dg_Environments.CurrentRow
Me.dg_Environments.Rows.Remove(curr_row)
Me.dg_Environments.Rows.Insert(curr_index + i, curr_row)
Me.dg_Environments.CurrentCell = Me.dg_Environments(curr_cell_index, curr_index + i)
End If
Catch ex As Exception
‘ do nothing if error encountered while trying to move the row up or down
End Try
End Sub

11 comments on “Move an unbound DataGridView row up or down via code (VB.NET)

  1. Not sure if this happens to you but the code you posted caused rows to disappear when you move up or down beyond the limits of the rowcount.

    I added:
    If (curr_index = Me.Grid1.RowCount – 1 And i = 1) Or (curr_index = 0 And i = -1) Then Exit Sub

    after the call to get the curr_index. This resolved the issue on my side…

  2. Thanks a lot bro, this code is awesome.
    just wondering if you have any similar code that moves a group of rows?
    Or can I just loop this code through the selected rows?

    Again, thanks a ton.

  3. If you change the down button so that the “Me.dgv_FinishingScheduleColumns.RowCount – 1” ends with -2 instead of -1 it solves the problem of deleting rows.

Leave a comment