Results 1 to 11 of 11

Thread: How do I move data in a DataGridView DOWN?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    Minneapolis, MN
    Posts
    531

    How do I move data in a DataGridView DOWN?

    Hello:

    If I want to move the row down, it only works the first time. If I hit the command a second time, it fails. The issue has to do with
    Code:
    Me.dgvScheduleData.Rows.Remove(myRow)
    because if I exclude this line, everything behaves as expected, but there is obviously duplicated data.

    Code:
                    Dim dat As Data.DataTable = Me.dgvScheduleData.DataSource
                    Dim myRow As DataGridViewRow = Me.dgvScheduleData.CurrentRow
                    Dim currow As Integer = Me.dgvScheduleData.CurrentCell.RowIndex
                    Dim newrow As DataRow = dat.NewRow
    
                    Dim lastrow As Integer = Me.dgvScheduleData.Rows.Count - 2
    
                    If currow < lastrow Then
                        For i As Integer = 0 To Me.dgvScheduleData.ColumnCount - 1
                            newrow.Item(i) = Me.dgvScheduleData(i, currow).Value
    
                        Next
    
                        dat.Rows.InsertAt(newrow, currow + 2)
    
                        Me.dgvScheduleData.Rows.Remove(myRow)
    
                        Me.dgvScheduleData.DataSource = dat
    
                        Me.dgvScheduleData.Refresh()
                        Me.dgvScheduleData.Rows(currow + 2).Selected = True
                        Me.dgvScheduleData.CurrentCell = dgvScheduleData.Rows(currow + 2).Cells(0)
    ------



    Quote Originally Posted by Gvincent View Post
    Or if the datagridview is binded:

    Dim dat As DataTable = Me.DataGridView1.DataSource
    Dim myRow As DataGridViewRow = Me.DataGridView1.CurrentRow

    Dim curRow As Integer = Me.DataGridView1.CurrentCell.RowIndex

    Dim newRow As DataRow = dat.NewRow

    For i As Integer = 0 To Me.DataGridView1.ColumnCount - 1
    newRow.Item(i) = Me.DataGridView1(i, curRow).Value
    Next

    dat.Rows.InsertAt(newRow, curRow - 1)

    Me.DataGridView1.Rows.Remove(myRow)

    Me.DataGridView1.DataSource = dat

    Me.DataGridView1.Refresh()
    Me.DataGridView1.Rows(curRow - 1).Selected = True
    Me.DataGridView1.CurrentCell = DataGridView1.Rows(curRow - 1).Cells(0)
    - A 'Hyperactive Member' trying to make a difference in a hyperactive world! And recently, I've been promoted to a 'Finatic Member,' whatever that means!

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: How do I move data in a DataGridView DOWN?

    When it fails, is there an error message, or does it just do nothing at all?
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    Minneapolis, MN
    Posts
    531

    Re: How do I move data in a DataGridView DOWN?

    I'm sorry for not being more specific. The question continues from this post: https://www.vbforums.com/showthread....taGridView-row

    The first two times, the code moves the new row down two rows as desired. The third time, it only moves it down one row. So there's no error message, it just changes the math. The variables are based on the currently selected row, and it only happens once when the command is initiated, so I am a bit confused.

    In the code below...
    The first time when the third row is selected (option base 0), currrow+2 = 5 and myrow = 3. This is correct.
    The second time as well.
    The third time, currrow+2 = 5 and myrow = 4. This is the problem.

    I am reselecting this row each time and then running the command to move the row down. The DataGridView is updated each time, but because the rows are not moved down properly, things start getting a little out of the desired order.

    Code:
                    Dim dat As Data.DataTable = Me.dgvScheduleData.DataSource
                    Dim myRow As DataGridViewRow = Me.dgvScheduleData.CurrentRow
                    Dim currow As Integer = Me.dgvScheduleData.CurrentCell.RowIndex
                    Dim newrow As DataRow = dat.NewRow
    
                    Dim lastrow As Integer = Me.dgvScheduleData.Rows.Count - 2
    
                    If currow < lastrow Then
                        For i As Integer = 0 To Me.dgvScheduleData.ColumnCount - 1
                            newrow.Item(i) = Me.dgvScheduleData(i, currow).Value
    
                        Next
    
                        MessageBox.Show(currow + 2)
                        dat.Rows.InsertAt(newrow, currow + 2)
    
                        MessageBox.Show(myRow.Index)
                        Me.dgvScheduleData.Rows.Remove(myRow)
    
                        Me.dgvScheduleData.DataSource = dat
    
                        Me.dgvScheduleData.Refresh()
                        Me.dgvScheduleData.Rows(currow + 2).Selected = True
                        Me.dgvScheduleData.CurrentCell = dgvScheduleData.Rows(currow + 2).Cells(0)
    
                    End If
    Thanks again!
    Last edited by ssabc; May 10th, 2021 at 01:32 PM.
    - A 'Hyperactive Member' trying to make a difference in a hyperactive world! And recently, I've been promoted to a 'Finatic Member,' whatever that means!

  4. #4
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: How do I move data in a DataGridView DOWN?

    You make a copy of the row at index currow. You insert this row at index currow+2, so the new current row is at currow+2.
    You then delete the original row at index currow. This moves all the following rows up 1, so the new current row index is now currow+1.

    In other words, the + 2 in the following lines should be + 1:
    Code:
    Me.dgvScheduleData.Rows(currow + 2).Selected = True
    Me.dgvScheduleData.CurrentCell = dgvScheduleData.Rows(currow + 2).Cells(0)

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    Minneapolis, MN
    Posts
    531

    Re: How do I move data in a DataGridView DOWN?

    Quote Originally Posted by Inferrd View Post
    You make a copy of the row at index currow. You insert this row at index currow+2, so the new current row is at currow+2.
    You then delete the original row at index currow. This moves all the following rows up 1, so the new current row index is now currow+1.

    In other words, the + 2 in the following lines should be + 1:
    Code:
    Me.dgvScheduleData.Rows(currow + 2).Selected = True
    Me.dgvScheduleData.CurrentCell = dgvScheduleData.Rows(currow + 2).Cells(0)

    I made the changes, but the logic still only works on the first two runs.
    Last edited by ssabc; May 10th, 2021 at 04:11 PM.
    - A 'Hyperactive Member' trying to make a difference in a hyperactive world! And recently, I've been promoted to a 'Finatic Member,' whatever that means!

  6. #6
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: How do I move data in a DataGridView DOWN?

    Quote Originally Posted by ssabc View Post
    It has to be +2, because going +1 and deleting the original does not change the order. There is no loop. After everything is moved, it should work the second time, with all the variables reset.

    I made the changes, but the logic still only works on tie first two runs.
    It works perfectly for me when using a button to do the moving. Here's the code I'm using, which is your code from post#3 with the last two lines altered to use the + 1.
    Code:
    Private Sub Button1_Click(sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        Dim dat As Data.DataTable = CType(Me.dgvScheduleData.DataSource, DataTable)
        Dim myRow As DataGridViewRow = Me.dgvScheduleData.CurrentRow
        Dim currow As Integer = Me.dgvScheduleData.CurrentCell.RowIndex
        Dim newrow As DataRow = dat.NewRow
    
        Dim lastrow As Integer = Me.dgvScheduleData.Rows.Count - 2
    
        If currow < lastrow Then
            For i As Integer = 0 To Me.dgvScheduleData.ColumnCount - 1
                newrow.Item(i) = Me.dgvScheduleData(i, currow).Value
    
            Next
    
            MessageBox.Show((currow + 2).ToString)
            dat.Rows.InsertAt(newrow, currow + 2)
    
            MessageBox.Show(myRow.Index.ToString)
            Me.dgvScheduleData.Rows.Remove(myRow)
    
            Me.dgvScheduleData.DataSource = dat
    
            Me.dgvScheduleData.Refresh()
            Me.dgvScheduleData.Rows(currow + 1).Selected = True
            Me.dgvScheduleData.CurrentCell = dgvScheduleData.Rows(currow + 1).Cells(0)
    
        End If
    
    End Sub

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    Minneapolis, MN
    Posts
    531

    Re: How do I move data in a DataGridView DOWN?

    Does it work if you hit the button a second time?
    - A 'Hyperactive Member' trying to make a difference in a hyperactive world! And recently, I've been promoted to a 'Finatic Member,' whatever that means!

  8. #8
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: How do I move data in a DataGridView DOWN?

    Quote Originally Posted by ssabc View Post
    Does it work if you hit the button a second time?
    Yes. If I select the 4th row and click the button 4 times, then I see the following message box messages (2 messages per click):

    5, 3
    6, 4
    7, 5
    8, 6

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    Minneapolis, MN
    Posts
    531

    Re: How do I move data in a DataGridView DOWN?

    Realize, I am using this with bound data. I am only seeing the data move the first time.

    The second time around it moves the data down one row instead of two...

    This entire thing is one huge cluster! Not sure I have another 40 hours to figure it out...

    Hypothetically, I want to move row 3 (option base 0) down to row 5, and delete row 3. It works great on the first run. But then I want to perhaps do it a second time on this or any selected row, and it only moves the row down one, and then deletes it.

    I have simplified the code below.

    Code:
        Private Sub MoveDown2()
            Try
                Dim dat As Data.DataTable = Me.dgvScheduleData.DataSource
                Dim newrow As DataRow = dat.NewRow
    
                Dim lastrow As Integer = Me.dgvScheduleData.Rows.Count - 2
    
                If Me.dgvScheduleData.CurrentCell.RowIndex < lastrow Then
                    For i As Integer = 0 To Me.dgvScheduleData.ColumnCount - 1
                        newrow.Item(i) = Me.dgvScheduleData(i, Me.dgvScheduleData.CurrentCell.RowIndex).Value
    
                    Next
    
                    dat.Rows.InsertAt(newrow, Me.dgvScheduleData.CurrentCell.RowIndex + 2)
    
                    Me.dgvScheduleData.DataSource = dat
                    Me.dgvScheduleData.Refresh()
    
                    Me.dgvScheduleData.Rows(Me.dgvScheduleData.CurrentCell.RowIndex + 2).Selected = True
                    Me.dgvScheduleData.CurrentCell = dgvScheduleData.Rows(Me.dgvScheduleData.CurrentCell.RowIndex + 2).Cells(0)
                    Me.dgvScheduleData.Rows.RemoveAt(Me.dgvScheduleData.CurrentCell.RowIndex - 2)
    
                End If
    
                Renum(dgvScheduleData)
    
            Catch
    
            End Try
    
        End Sub
    Last edited by ssabc; Jun 3rd, 2021 at 12:36 PM.
    - A 'Hyperactive Member' trying to make a difference in a hyperactive world! And recently, I've been promoted to a 'Finatic Member,' whatever that means!

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How do I move data in a DataGridView DOWN?

    If the data is bound then you shouldn't be trying to move rows manually in the grid at all. Here's what you should do:

    1. Add an extra column to the DataTable named SortOrder of type Integer.
    2. Add increasing sequential values to that column.
    3. Bind your DataTable to a BindingSource.
    4. Bind your BindingSource to the grid.
    5. If you auto-generate the grid columns, hide the one for SortOrder. Otherwise, create the other columns manually and don't create one for SortOrder.
    6. Set the Sort property of the BindingSource to "SortOrder".
    7. When you want to move a row, simply change the SortOrder values, e.g. to move the current row up:
    vb.net Code:
    1. Dim currentRow = DirectCast(myBindingSource.Current, DataRowView)
    2. Dim previousRow = DirectCast(myBindingSource(myBindingSource.Position - 1), DataRowView)
    3.  
    4. Dim currentSortOrder = currentRow("SortOrder")
    5. Dim previousSortOrder = previousRow("SortOrder")
    6.  
    7. currentRow("SortOrder") = previousSortOrder
    8. previousRow("SortOrder") = currentSortOrder

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How do I move data in a DataGridView DOWN?

    If you then want to sort by a different column, you sort by that column first, then renumber the SortOrder column, then sort by SortOrder again, e.g.
    vb.net Code:
    1. myBindingSource.SuspendBinding()
    2. myBindingSource.Sort = "Name"
    3.  
    4. For i = 0 To myBindingSource.Count - 1
    5.     DirectCast(myBindingSource(i), DataRowView)("SortOrder") = i
    6. Next
    7.  
    8. myBindingSource.Sort = "SortOrder"
    9. myBindingSource.ResumeBinding()

Posting Permissions

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



Click Here to Expand Forum to Full Width