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:
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:
VB.NET:
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