Buongiorno a tutti, ho un DataGridView collegato a un DataSource. Tramite il seguente codice modifico l'ordine delle righe tramite drag&drop:
Private Sub DGV_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles DGV.MouseMove
If (e.Button And MouseButtons.Left) = MouseButtons.Left Then
If dragBoxFromMouseDown <> Rectangle.Empty AndAlso Not dragBoxFromMouseDown.Contains(e.X, e.Y) Then
Dim dropEffect As DragDropEffects = DGV.DoDragDrop(DGV.Rows(rowIndexFromMouseDown), DragDropEffects.Move)
End If
End If
End Sub
Private Sub DGV_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles DGV.MouseDown
rowIndexFromMouseDown = DGV.HitTest(e.X, e.Y).RowIndex
If rowIndexFromMouseDown <> -1 Then
Dim dragSize As Size = SystemInformation.DragSize
dragBoxFromMouseDown = New Rectangle(New Point(CInt(e.X - (dragSize.Width / 2)), CInt(e.Y - (dragSize.Height / 2))), dragSize)
Else
dragBoxFromMouseDown = Rectangle.Empty
End If
End Sub
Private Sub DGV_DragOver(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles DGV.DragOver
e.Effect = DragDropEffects.Move
End Sub
Private Sub DGV_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles DGV.DragDrop
Dim clientPoint As Point = DGV.PointToClient(New Point(e.X, e.Y))
rowIndexOfItemUnderMouseToDrop = DGV.HitTest(clientPoint.X, clientPoint.Y).RowIndex
If e.Effect = DragDropEffects.Move Then
Dim rowToMove As DataGridViewRow = TryCast(e.Data.GetData(GetType(DataGridViewRow)), DataGridViewRow)
Dim dt As DataTable = DS.Tables("Tabella")
Dim drArray() As Object = dt.Rows(rowToMove.Index).ItemArray
Dim dr As DataRow = dt.NewRow
dr.ItemArray = drArray
dt.Rows.RemoveAt(rowToMove.Index)
dt.Rows.InsertAt(dr, rowIndexOfItemUnderMouseToDrop)
dr.AcceptChanges()
End If
End Sub
Il tutto funziona bene solo che non riesco a salvare le righe spostate nel database.
Quindi la domanda è: come faccio a salvare le righe ordinate nel database?
Grazie.