I've looked online and I've seen a lot of different posts of similar stuff. My code moves the rows correctly.
This is what happens: I press either the UP or Down Buttons, the selected rows moves accordingly.
If I click on the Row that I just moved and I try to move it back, it moves it in the DataTable's DefaultView sort order but the position change is NOT reflected in the DataGridView.
If I select a Row I've never moved, it will move it and the DataGridView will reflect it, but once again if I choose that same row and try to move it back, this change is not reflected in the View, but the DataTable is updated.
I've tried to ResetBindings, DataGridView.Refresh, reset the DataSource: none of this works.
Anyone know why this happens and how i can fix it so it reflects the changes every time?
Private Sub UpBtn_Click(sender As Object, e As EventArgs) Handles UpBtn.Click
MoveUpDataRow(StepDGV.CurrentRow.Index)
End Sub
Private Sub MoveUpDataRow(RowIndex As Integer)
Dim OriginalStepNumber As Integer
Dim dv As DataView = StepsData.DefaultView
OriginalStepNumber = CInt(StepDGV.Rows(RowIndex).Cells("StepIDLS").Value)
If RowIndex = 0 Then
'dv(RowIndex - 1)("StepIDLS") = OriginalStepNumber + 1
'dv(RowIndex)("StepIDLS") = OriginalStepNumber - 1
exit Sub
Else
dv(RowIndex - 1).BeginEdit()
dv(RowIndex - 1)("StepIDLS") = OriginalStepNumber
'dv(RowIndex)("StepIDLS") = RowIndex-1
dv(RowIndex - 1).EndEdit()
dv(RowIndex).BeginEdit()
dv(RowIndex)("StepIDLS") = OriginalStepNumber - 1
'dv(RowIndex)("StepIDLS") = RowIndex-1
dv(RowIndex).EndEdit()
End If
dv.Sort = "StepIDLS ASC"
' StepDGV.Rows(RowIndex).Selected = true
'StepDGV.DataSource = StepsData
'StepDGV.Refresh()
StepDGV.ResetBindings()
StepDGV.Refresh()
End Sub
Private Sub downBtn_Click(sender As Object, e As EventArgs) Handles downBtn.Click
MoveDownDataRow(StepDGV.CurrentRow.Index)
End Sub
Private Sub MoveDownDataRow(RowIndex As Integer)
Dim OriginalStepNumber As Integer
Dim dv As DataView = StepsData.DefaultView
OriginalStepNumber = CInt(StepDGV.Rows(RowIndex).Cells("StepIDLS").Value)
If RowIndex = dv.Count - 1 Then
exit Sub
Else
dv(RowIndex + 1).BeginEdit()
dv(RowIndex + 1)("StepIDLS") = OriginalStepNumber
dv(RowIndex + 1).EndEdit()
dv(RowIndex).BeginEdit()
dv(RowIndex)("StepIDLS") = OriginalStepNumber + 1
dv(RowIndex).EndEdit()
End If
dv.Sort = "StepIDLS ASC"
' StepDGV.Rows(RowIndex).Selected = true
'StepDGV.Refresh()
'StepDGV.DataSource = StepsData
'StepDGV.Refresh()
StepDGV.ResetBindings()
StepDGV.Refresh()
End Sub[![screenshot of datagridview for visual aid when going through code][1]][1]