Question Drag & Drop Between 2 Datagrids Between 2 MDI Child Forms

pidyok

Member
Joined
Feb 10, 2009
Messages
19
Programming Experience
Beginner
Ive been working on this project of mine for some time now... though im not involved in IT in my current company, im just trying to help to at least have their system computerized (they are still doing it manually by paper & calculator)... I have one problem though which made me stick for 2 days (and still counting) without progress... How To Drag & Drop Between 2 Datagrids Between 2 similar MDI Child Forms >>> anybody can help me with the above topic??? my apologies, im not that adept in VB.net programming... thank you very much... :(:(:(
 

Attachments

  • Manifest.jpg
    Manifest.jpg
    52.5 KB · Views: 79
When you have many runtime created controls and need them to function the same way you use the Addhandler statement so they all use the events as above, then you don't need the Handles clause at the end of the event.
VB.NET:
Dim newfrm As New mdiChildForm
Addhandler newfrm.dgv.dragdrop , AddressOf <your dragdrop sub>
Also name each dgv, declare a variable, set a breakpoint and have this variable grab the dgv's name then in the debugger going line by line - after it executes the line with this variable you can get the name when you hover your mouse over the variable and it will like-intellisence(or whatever you call it) the value then you can see what the name is.
 
Last edited:
dragdrop row from datagridview of a childform to another datagridview of a childform

I finally got it working... by trying to rename the datagrid on creation of the child form...

Private Sub ShowNewForm(ByVal sender As Object, ByVal e As EventArgs) Handles NewToolStripMenuItem.Click, NewToolStripButton.Click, NewWindowToolStripMenuItem.Click

m_ChildFormNumber += 1
Dim ChildForm As New frmManifest
ChildForm.MdiParent = Me

ChildForm.Text = "Manifest " & m_ChildFormNumber
ChildForm.Name = "frmManifest" & m_ChildFormNumber
ChildForm.dgv1.Name = "dgv" & m_ChildFormNumber '<<< rename datagridview

ChildForm.Show()​

End Sub

by placing the datagrid's name on a mousedown event in a public variable {iGridSourceName} i was able to identify if i am still dropping on the same form/datagridview... this allows unecessary drop on the source datagrid on dragdrop subroutine.... though one thing that got me wondering was the handle name "dgv1.***" >> which is the original name of the grid IS WORKING on all other forms/grids created subsequently at runtime (even though i renamed them already)... anyway, im posting the code as there's no drag drop of a row from one datagrid view of a child form to another datagrid view of another child form... MANY THANKS TO NEWGUY (rep added) & markjprichard (vbcity.com >>> hope im not violating forum rules here)....



Private Sub dgvOriginating_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgv1.MouseDown
Dim iSource As DataGridView = sender
iGridSourceName = iSource.Name
'Debug.WriteLine(iSource.Name)

dgv1.DoDragDrop(dgv1.SelectedRows, DragDropEffects.Copy)​
End Sub

Private Sub dgvDropping_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles dgv1.DragEnter
Dim i As Integer
For i = 0 To e.Data.GetFormats().Length - 1
If e.Data.GetFormats()(i).Equals("System.Windows.Forms.DataGridViewSelectedRowCollection") Then
e.Effect = DragDropEffects.Copy
End If
Next​
End Sub

Private Sub dgvDropping_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles dgv1.DragDrop

Dim iDest As DataGridView = sender
'Debug.WriteLine(iDest.Name)
If iGridSourceName = iDest.Name Then Exit Sub

Dim row, lclrow As DataGridViewRow
Dim ii As Integer
If e.Effect = DragDropEffects.Copy Then
For ii = 0 To e.Data.GetFormats().Length - 1
Console.WriteLine(e.Data.GetFormats()(ii).ToString)
Next​
Dim rc As DataGridViewSelectedRowCollection = e.Data.GetData("System.Windows.Forms.DataGridViewSelectedRowCollection")
For Each row In rc
lclrow = New DataGridViewRow
lclrow.CreateCells(dgv1)
For ii = 0 To lclrow.Cells.Count - 1
lclrow.Cells(ii).Value = row.Cells(ii).Value
Next​
dgv1.Rows.Add(lclrow)
Next​
End If​
End Sub​
 
Back
Top