Link to home
Create AccountLog in
Avatar of David Svedarsky
David SvedarskyFlag for United States of America

asked on

Sample code for bound Datagridview Drag and Drop

Hi Experts,

I need sample code for a bound Datagridview Drag and Drop.

Windows XP
Visual Studio 2005
SQL Server 2005
VB.Net

Thanks,

Dave
Avatar of vadim63
vadim63
Flag of United States of America image

DO you mean rows reordering?
Avatar of David Svedarsky

ASKER

Sorry...

The title should read:
"Drag and drop reordering of rows in databound DataGridView"
Public Class Form1

    Dim dragBoxFromMouseDown As Rectangle
    Dim rowIndexFromMouseDown, rowIndexOfItemUnderMouseToDrop As Integer

    Private Sub DataGridView1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop
        Dim clientPoint As Point = DataGridView1.PointToClient(New Point(e.X, e.Y))
        rowIndexOfItemUnderMouseToDrop = DataGridView1.HitTest(clientPoint.X, clientPoint.Y).RowIndex
        If e.Effect = DragDropEffects.Move Then
            Dim rowToMove As DataGridViewRow
            rowToMove = e.Data.GetData(GetType(DataGridViewRow))
            DataGridView1.Rows.RemoveAt(rowIndexFromMouseDown)
            DataGridView1.Rows.Insert(rowIndexOfItemUnderMouseToDrop, rowToMove)
        End If
    End Sub

    Private Sub DataGridView1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragOver
        e.Effect = DragDropEffects.Move
    End Sub

    Private Sub DataGridView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
        rowIndexFromMouseDown = DataGridView1.HitTest(e.X, e.Y).RowIndex
        If rowIndexFromMouseDown <> -1 Then
            Dim dragSize As Size = SystemInformation.DragSize
            dragBoxFromMouseDown = New Rectangle(New Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)), dragSize)
        Else
            dragBoxFromMouseDown = Rectangle.Empty
        End If
    End Sub

    Private Sub DataGridView1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseMove
        If e.Button = Windows.Forms.MouseButtons.Left Then
            If dragBoxFromMouseDown <> Rectangle.Empty And dragBoxFromMouseDown.Contains(e.X, e.Y) Then
                Dim dropEffect As DragDropEffects = DataGridView1.DoDragDrop(DataGridView1.Rows(rowIndexFromMouseDown), DragDropEffects.Move)
            End If
        End If
    End Sub
End Class
vadim63,

I tried the code, a row will drag and drop.
But the row disappears.

What property settings do I use?

Thanks,

Dave
There's only one property:
Me.DataGridView1.AllowDrop = True
Where you try to drop the row?
vadim63,

I just noticed that if I move a row it disappears and if I update the database the row is deleted.
I did have that property setting.

The Primary Key's data type is Int with Autoincrement = True

I am trying to drop the row in the same datagridview.
Are you trying to create a new row, or just reorder it?
I am trying to reorder, not create a new row.

I assume my definition of "reorder" is correct, meaning changing the order of the Datagridview rows.


ASKER CERTIFIED SOLUTION
Avatar of vadim63
vadim63
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
I found the change that is needed.

Instead of removing and inserting in the DataGridView, remove and insert into the DataTable.

Change:
Dim rowToMove As DataGridViewRow
         rowToMove = e.Data.GetData(GetType(DataGridViewRow))
         Datagridview1.Rows.RemoveAt(rowIndexFromMouseDown)
          Datagridview1.Rows.Insert(rowIndexOfItemUnderMouseToDrop, rowToMove)

To:
         'get the row data from row before removing and add to new row
         Dim rowArray As Object() = DataSetName.TableName.Rows(rowIndexFromMouseDown).ItemArray
         Dim row As DataRow = DataSetName.TableName.NewRow()
         row.ItemArray = rowArray

         ' remove old row and insert new row
         DataSetName.TableName.Rows.RemoveAt(rowIndexFromMouseDown)
         DataSetName.TableName.Rows.InsertAt(row, rowIndexOfItemUnderMouseToDrop)

It works!

I have tried to get this working several times in the past with no luck. But with your help we got it done.

Thanks a lot,

Dave
You're welcome.