Results 1 to 4 of 4

Thread: moving datagridview rows up and down with button click

  1. #1
    ud2008 is offline VB.NET Forum Fanatic
    .NET Framework
    .NET 4.0
    Join Date
    Jul 2010
    Posts
    148
    Reputation
    56

    moving datagridview rows up and down with button click

    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:
    Code:
    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

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    12,942
    Reputation
    1625
    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.

  3. #3
    ud2008 is offline VB.NET Forum Fanatic
    .NET Framework
    .NET 4.0
    Join Date
    Jul 2010
    Posts
    148
    Reputation
    56
    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

  4. #4
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    12,942
    Reputation
    1625
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking