Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15714

VS 2017 How do I move data in a DataGridView DOWN?

$
0
0
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)


Viewing all articles
Browse latest Browse all 15714

Trending Articles