Question moving datagridview rows up and down with button click

ud2008

Well-known member
Joined
Jul 5, 2010
Messages
148
Programming Experience
Beginner
I use vb2010.
I have a datagridview (which gets it's records from a mysql database), 2 buttons (up and down) on a form.

What I want to do is to be able to move a selected row up or down (not just a selection but reordering the rows).

The code I have doesn't work, what it does is removing rows from the grid.

Please any help would be great.

This is the code I use:
VB.NET:
Private Sub MoveRow(ByVal i As Integer)
        Try
            If (Me.DataGridViewX1.SelectedCells.Count > 0) Then
                Dim curr_index As Integer = Me.DataGridViewX1.CurrentCell.RowIndex
                Dim curr_col_index As Integer = Me.DataGridViewX1.CurrentCell.ColumnIndex
                Dim curr_row As DataGridViewRow = Me.DataGridViewX1.CurrentRow
                Me.DataGridViewX1.Rows.Remove(curr_row)
                Me.DataGridViewX1.Rows.Insert(curr_index + i, curr_row)
                Me.DataGridViewX1.CurrentCell = Me.DataGridViewX1(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

    Private Sub UpButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpButton.Click
        MoveRow(-1) ' move up in the datagridview (row index is 1 less)
    End Sub

    Private Sub DownButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DownButton.Click
        MoveRow(+1) ' move down in the datagridview (row index is 1 more)
    End Sub
 
If you're getting your data from a database then you should be populating a DataTable and simply binding that to the grid via a BindingSource. The easy option to move a row is then to add an extra column to the DataTable containing numbers and sorting the BindingSource on that column. Reordering rows is then a simple matter of swapping values in that column. The data will be automatically re-sorted.
 
I think my datagrid is unbound, because I only once receive the data from the database and when I add new records to the grid it not sync with the database (which is not needed).

So the data is unbound and I want to be able to recorder the rows in the grid, for me and for the person who will use this program it is easier to use buttons.

Or do you have an idea on how to use a listbox, because I can use a listbox to reorder the items, "multi column", but then need to store them back into the datagrid.

Thanks
 
I know your grid is unbound. I'm saying that, while that's OK if that's what you want, it would make more that it be bound. That would make what you want to do now extremely simple.
 
VB.NET:
    Private Sub MoveRow(ByVal i As Integer)
        Try
            If (Me.Grid1.SelectedCells.Count > 0) Then
                Dim curr_index As Integer = Me.Grid1.CurrentCell.RowIndex
                Dim curr_col_index As Integer = Me.Grid1.CurrentCell.ColumnIndex
                Dim curr_row As DataGridViewRow = Me.Grid1.CurrentRow
                Me.Grid1.Rows.Remove(curr_row)
                Me.Grid1.Rows.Insert(curr_index + i, curr_row)
                Me.Grid1.CurrentCell = Me.Grid1(curr_col_index, curr_index + i)


            End If
        Catch ex As Exception

            '  MsgBox("First Record cannot be Moved Up")
            ' do nothing if error encountered while trying to move the row up or down
        End Try
    End Sub

    Private Sub UpButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnUp.Click
        If Me.Grid1.CurrentCell.RowIndex > 0 Then
            MoveRow(-1) ' move up in the datagridview (row index is 1 less
        End If
    End Sub

    Private Sub DownButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDown.Click
        If Me.Grid1.CurrentCell.RowIndex < Grid1.Rows.Count - 1 Then
            MoveRow(+1) ' move down in the datagridview (row index is 1 more)
        End If
    End Sub
 
Last edited by a moderator:
Back
Top