I probably am missing something blindingly obvious here, but try as I might, I can't find the solution to this issue...
I have a DGV bound to a bindingsource. I want to be able to move rows within the DGV, each of which has a "classindex" column. The bindingsource is sorted on this column.
I tried programming a drag-drop system to re-order the rows, but ended up with two buttons btnMoveUp and btnMoveDown.
What is supposed to happen when you click "Up" is that the selected row "classindex" value swaps with the row above and so moves up one row. The opposite happens with the "Down" button.
All works fine until you click "Up" after clicking "Down" or vice versa. It all seems to go a bit pear shaped then and I can't work out why.
Here is my code for the two buttons...
intRowSelected is declared as a public integer
Can someone spot the flaw please??
Thanks.
Tim
I have a DGV bound to a bindingsource. I want to be able to move rows within the DGV, each of which has a "classindex" column. The bindingsource is sorted on this column.
I tried programming a drag-drop system to re-order the rows, but ended up with two buttons btnMoveUp and btnMoveDown.
What is supposed to happen when you click "Up" is that the selected row "classindex" value swaps with the row above and so moves up one row. The opposite happens with the "Down" button.
All works fine until you click "Up" after clicking "Down" or vice versa. It all seems to go a bit pear shaped then and I can't work out why.
Here is my code for the two buttons...
intRowSelected is declared as a public integer
VB.NET:
Private Sub btnMoveUp_Click(sender As Object, e As EventArgs) Handles btnMoveUp.Click
With dgvEventcategories
intRowSelected = .CurrentRow.Index
If intRowSelected = 0 Then Exit Sub ' top of list
Dim intTemp As Integer = .Rows.Item(intRowSelected - 1).Cells("classindex").Value
.Rows.Item(intRowSelected - 1).Cells("classindex").Value = .Rows.Item(intRowSelected).Cells("classindex").Value
.Rows.Item(intRowSelected).Cells("classindex").Value = intTemp
.CurrentCell = .Rows.Item(intRowSelected - 1).Cells("classindex")
.CurrentRow.Selected = True
End With
End Sub
Private Sub btnMoveDown_Click(sender As Object, e As EventArgs) Handles btnMoveDown.Click
With dgvEventcategories
intRowSelected = .CurrentRow.Index
If intRowSelected = EventcategoriesBindingSource.Count - 1 Then Exit Sub ' end of list
Dim intTemp As Integer = .Rows.Item(intRowSelected + 1).Cells("classindex").Value
.Rows.Item(intRowSelected + 1).Cells("classindex").Value = .Rows.Item(intRowSelected).Cells("classindex").Value
.Rows.Item(intRowSelected).Cells("classindex").Value = intTemp
.CurrentCell = .Rows.Item(intRowSelected + 1).Cells("classindex")
.CurrentRow.Selected = True
End With
End Sub
Can someone spot the flaw please??
Thanks.
Tim