David Svedarsky
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
I need sample code for a bound Datagridview Drag and Drop.
Windows XP
Visual Studio 2005
SQL Server 2005
VB.Net
Thanks,
Dave
DO you mean rows reordering?
ASKER
Sorry...
The title should read:
"Drag and drop reordering of rows in databound DataGridView"
The title should read:
"Drag and drop reordering of rows in databound DataGridView"
Public Class Form1
Dim dragBoxFromMouseDown As Rectangle
Dim rowIndexFromMouseDown, rowIndexOfItemUnderMouseTo Drop As Integer
Private Sub DataGridView1_DragDrop(ByV al sender As Object, ByVal e As System.Windows.Forms.DragE ventArgs) Handles DataGridView1.DragDrop
Dim clientPoint As Point = DataGridView1.PointToClien t(New Point(e.X, e.Y))
rowIndexOfItemUnderMouseTo Drop = DataGridView1.HitTest(clie ntPoint.X, clientPoint.Y).RowIndex
If e.Effect = DragDropEffects.Move Then
Dim rowToMove As DataGridViewRow
rowToMove = e.Data.GetData(GetType(Dat aGridViewR ow))
DataGridView1.Rows.RemoveA t(rowIndex FromMouseD own)
DataGridView1.Rows.Insert( rowIndexOf ItemUnderM ouseToDrop , rowToMove)
End If
End Sub
Private Sub DataGridView1_DragOver(ByV al sender As Object, ByVal e As System.Windows.Forms.DragE ventArgs) Handles DataGridView1.DragOver
e.Effect = DragDropEffects.Move
End Sub
Private Sub DataGridView1_MouseDown(By Val sender As Object, ByVal e As System.Windows.Forms.Mouse EventArgs) 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(By Val sender As Object, ByVal e As System.Windows.Forms.Mouse EventArgs) Handles DataGridView1.MouseMove
If e.Button = Windows.Forms.MouseButtons .Left Then
If dragBoxFromMouseDown <> Rectangle.Empty And dragBoxFromMouseDown.Conta ins(e.X, e.Y) Then
Dim dropEffect As DragDropEffects = DataGridView1.DoDragDrop(D ataGridVie w1.Rows(ro wIndexFrom MouseDown) , DragDropEffects.Move)
End If
End If
End Sub
End Class
Dim dragBoxFromMouseDown As Rectangle
Dim rowIndexFromMouseDown, rowIndexOfItemUnderMouseTo
Private Sub DataGridView1_DragDrop(ByV
Dim clientPoint As Point = DataGridView1.PointToClien
rowIndexOfItemUnderMouseTo
If e.Effect = DragDropEffects.Move Then
Dim rowToMove As DataGridViewRow
rowToMove = e.Data.GetData(GetType(Dat
DataGridView1.Rows.RemoveA
DataGridView1.Rows.Insert(
End If
End Sub
Private Sub DataGridView1_DragOver(ByV
e.Effect = DragDropEffects.Move
End Sub
Private Sub DataGridView1_MouseDown(By
rowIndexFromMouseDown = DataGridView1.HitTest(e.X,
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(By
If e.Button = Windows.Forms.MouseButtons
If dragBoxFromMouseDown <> Rectangle.Empty And dragBoxFromMouseDown.Conta
Dim dropEffect As DragDropEffects = DataGridView1.DoDragDrop(D
End If
End If
End Sub
End Class
ASKER
vadim63,
I tried the code, a row will drag and drop.
But the row disappears.
What property settings do I use?
Thanks,
Dave
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?
Me.DataGridView1.AllowDrop
Where you try to drop the row?
ASKER
vadim63,
I just noticed that if I move a row it disappears and if I update the database the row is deleted.
I just noticed that if I move a row it disappears and if I update the database the row is deleted.
ASKER
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.
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?
ASKER
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.
I assume my definition of "reorder" is correct, meaning changing the order of the Datagridview rows.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
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(Dat aGridViewR ow))
Datagridview1.Rows.RemoveA t(rowIndex FromMouseD own)
Datagridview1.Rows.Insert( rowIndexOf ItemUnderM ouseToDrop , rowToMove)
To:
'get the row data from row before removing and add to new row
Dim rowArray As Object() = DataSetName.TableName.Rows (rowIndexF romMouseDo wn).ItemAr ray
Dim row As DataRow = DataSetName.TableName.NewR ow()
row.ItemArray = rowArray
' remove old row and insert new row
DataSetName.TableName.Rows .RemoveAt( rowIndexFr omMouseDow n)
DataSetName.TableName.Rows .InsertAt( row, rowIndexOfItemUnderMouseTo Drop)
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
Instead of removing and inserting in the DataGridView, remove and insert into the DataTable.
Change:
Dim rowToMove As DataGridViewRow
rowToMove = e.Data.GetData(GetType(Dat
Datagridview1.Rows.RemoveA
Datagridview1.Rows.Insert(
To:
'get the row data from row before removing and add to new row
Dim rowArray As Object() = DataSetName.TableName.Rows
Dim row As DataRow = DataSetName.TableName.NewR
row.ItemArray = rowArray
' remove old row and insert new row
DataSetName.TableName.Rows
DataSetName.TableName.Rows
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.