tiehaze
asked on
Drag and Drop between two listviews (in 'Details' view)
I have a windows application in which I need to set up a drag and drop function between two listviews. Each listview has two columns. All I can find on the internet is how to do it in 'List' view, but I need the listviews to be in 'Details' view. Here is the code that I found to do it in 'List' view:
Private Sub ListView_ItemDrag(ByVal sender As Object, ByVal e As _
System.Windows.Forms.ItemD ragEventAr gs) Handles AccountsLV.ItemDrag, _
AssetsLV.ItemDrag
Dim myItem As ListViewItem
Dim myItems(sender.SelectedIte ms.Count - 1) As ListViewItem
Dim i As Integer = 0
' Loop though the SelectedItems collection for the source.
For Each myItem In sender.SelectedItems
' Add the ListViewItem to the array of ListViewItems.
myItems(i) = myItem
i = i + 1
Next
' Create a DataObject containg the array of ListViewItems.
sender.DoDragDrop(New _
DataObject("System.Windows .Forms.Lis tViewItem( )", myItems), _
DragDropEffects.Move)
End Sub
Private Sub ListView_DragEnter(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragE ventArgs) Handles AccountsLV.DragEnter, AssetsLV.DragEnter
' Check for the custom DataFormat ListViewItem array.
If e.Data.GetDataPresent("Sys tem.Window s.Forms.Li stViewItem ()") Then
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub ListView_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragE ventArgs) Handles AccountsLV.DragDrop, AssetsLV.DragDrop
Dim myItem As ListViewItem
Dim myItems() As ListViewItem = e.Data.GetData("System.Win dows.Forms .ListViewI tem()")
Dim i As Integer = 0
For Each myItem In myItems
' Add the item to the target list.
sender.Items.Add(myItems(i ).Text)
' Remove the item from the source list.
If sender Is AccountsLV Then
AssetsLV.Items.Remove(Asse tsLV.Selec tedItems.I tem(0))
Else
AccountsLV.Items.Remove(Ac countsLV.S electedIte ms.Item(0) )
End If
i = i + 1
Next
End Sub
I also found the following code which allows me to add items in the second listview with a button click event:
Private Sub ToolStripButton1_Click(ByV al sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
For Each item As ListViewItem In AccountsLV.SelectedItems
Dim newItem As ListViewItem = CType(item.Clone(), ListViewItem)
AssetsLV.Items.Add(newItem )
Next
End Sub
Is there anyway to combine the two somehow so that I can do a drag and drop, in 'Details' view?
Private Sub ListView_ItemDrag(ByVal sender As Object, ByVal e As _
System.Windows.Forms.ItemD
AssetsLV.ItemDrag
Dim myItem As ListViewItem
Dim myItems(sender.SelectedIte
Dim i As Integer = 0
' Loop though the SelectedItems collection for the source.
For Each myItem In sender.SelectedItems
' Add the ListViewItem to the array of ListViewItems.
myItems(i) = myItem
i = i + 1
Next
' Create a DataObject containg the array of ListViewItems.
sender.DoDragDrop(New _
DataObject("System.Windows
DragDropEffects.Move)
End Sub
Private Sub ListView_DragEnter(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragE
' Check for the custom DataFormat ListViewItem array.
If e.Data.GetDataPresent("Sys
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub ListView_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragE
Dim myItem As ListViewItem
Dim myItems() As ListViewItem = e.Data.GetData("System.Win
Dim i As Integer = 0
For Each myItem In myItems
' Add the item to the target list.
sender.Items.Add(myItems(i
' Remove the item from the source list.
If sender Is AccountsLV Then
AssetsLV.Items.Remove(Asse
Else
AccountsLV.Items.Remove(Ac
End If
i = i + 1
Next
End Sub
I also found the following code which allows me to add items in the second listview with a button click event:
Private Sub ToolStripButton1_Click(ByV
For Each item As ListViewItem In AccountsLV.SelectedItems
Dim newItem As ListViewItem = CType(item.Clone(), ListViewItem)
AssetsLV.Items.Add(newItem
Next
End Sub
Is there anyway to combine the two somehow so that I can do a drag and drop, in 'Details' view?
Trying to understand, you are able to drag and drop using the other views but not able to do the same with Details View?
ASKER
Yes, correct
Can you explain some more, because looking at the code it would work in details view to. Can you show us the code where you setup the listviews?
ASKER
It works, but all it is transferring is column 1.... I need both columns to transfer
You need to iterate the subitems e.g.
Private Sub ListView_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragE ventArgs) Handles AccountsLV.DragDrop, AssetsLV.DragDrop
Dim myItem As ListViewItem
Dim myItems() As ListViewItem = e.Data.GetData("System.Win dows.Forms .ListViewI tem()")
Dim i As Integer = 0
For Each myItem In myItems
' Add the item to the target list.
Dim itmX as new listviewitem
itmX.Text = myItems(i).Text
for ctr as integer = 0 to myItems(i).subitems.count - 1
itmX.Subitems.add(myItems( i).subitem s(ctr).tos tring
next ctr
sender.Items.Add(itmX)
' Remove the item from the source list.
If sender Is AccountsLV Then
AssetsLV.Items.Remove(Asse tsLV.Selec tedItems.I tem(0))
Else
AccountsLV.Items.Remove(Ac countsLV.S electedIte ms.Item(0) )
End If
i = i + 1
Next
End Sub
Private Sub ListView_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragE
Dim myItem As ListViewItem
Dim myItems() As ListViewItem = e.Data.GetData("System.Win
Dim i As Integer = 0
For Each myItem In myItems
' Add the item to the target list.
Dim itmX as new listviewitem
itmX.Text = myItems(i).Text
for ctr as integer = 0 to myItems(i).subitems.count - 1
itmX.Subitems.add(myItems(
next ctr
sender.Items.Add(itmX)
' Remove the item from the source list.
If sender Is AccountsLV Then
AssetsLV.Items.Remove(Asse
Else
AccountsLV.Items.Remove(Ac
End If
i = i + 1
Next
End Sub
ASKER
In Column 2, instead of adding the subitem text, it adds:
ListViewSubitem: {R0120006}
ListViewSubitem: {R0120009}
etc.
R0120006 and R0120009 are the items in column 1
Any idea how to fix this?
ListViewSubitem: {R0120006}
ListViewSubitem: {R0120009}
etc.
R0120006 and R0120009 are the items in column 1
Any idea how to fix this?
sorry the line should be
itmX.Subitems.add(myItems( i).subitem s(ctr)
(Without the ToString)
itmX.Subitems.add(myItems(
(Without the ToString)
ASKER
Now it is just repeating the items that are in column 1 in column 2:
R0120006 R0120006
R0120009 R0120009
R0120006 R0120006
R0120009 R0120009
Okay I double checked are you sure you have coded it right, as in you used the 'ctr' variable?
for ctr as integer = 0 to myItems(i).subitems.count - 1
itmX.Subitems.add(myItems( i).subitem s(ctr).tos tring)
next ctr
YOU DO NEED the ToString [My mistake, not reading the code)
for ctr as integer = 0 to myItems(i).subitems.count - 1
itmX.Subitems.add(myItems(
next ctr
YOU DO NEED the ToString [My mistake, not reading the code)
Paste your code here again please
ASKER
It went back to doing:
Column 2, instead of adding the subitem text, it adds:
ListViewSubitem: {R0120006}
ListViewSubitem: {R0120009}
etc.
R0120006 and R0120009 are the items in column 1
here is my code:
Private Sub ListView_ItemDrag(ByVal sender As Object, ByVal e As _
System.Windows.Forms.ItemD ragEventAr gs) Handles AccountsLV.ItemDrag, _
ListView1.ItemDrag
Dim myItem As ListViewItem
Dim myItems(sender.SelectedIte ms.Count - 1) As ListViewItem
Dim i As Integer = 0
' Loop though the SelectedItems collection for the source.
For Each myItem In sender.SelectedItems
' Add the ListViewItem to the array of ListViewItems.
myItems(i) = myItem
i = i + 1
Next
' Create a DataObject containg the array of ListViewItems.
sender.DoDragDrop(New _
DataObject("System.Windows .Forms.Lis tViewItem( )", myItems), _
DragDropEffects.Move)
End Sub
Private Sub ListView_DragEnter(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragE ventArgs) Handles AccountsLV.DragEnter, ListView1.DragEnter
' Check for the custom DataFormat ListViewItem array.
If e.Data.GetDataPresent("Sys tem.Window s.Forms.Li stViewItem ()") Then
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub ListView_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragE ventArgs) Handles AccountsLV.DragDrop, ListView1.DragDrop
Dim myItem As ListViewItem
Dim myItems() As ListViewItem = e.Data.GetData("System.Win dows.Forms .ListViewI tem()")
Dim i As Integer = 0
For Each myItem In myItems
' Add the item to the target list.
Dim itmX As New ListViewItem
itmX.Text = myItems(i).Text
For ctr As Integer = 0 To myItems(i).SubItems.Count - 1
itmX.SubItems.Add(myItems( i).SubItem s(ctr).ToS tring)
Next ctr
sender.Items.Add(itmX)
' Remove the item from the source list.
If sender Is AccountsLV Then
ListView1.Items.Remove(Lis tView1.Sel ectedItems .Item(0))
Else
AccountsLV.Items.Remove(Ac countsLV.S electedIte ms.Item(0) )
End If
i = i + 1
Next
End Sub
Column 2, instead of adding the subitem text, it adds:
ListViewSubitem: {R0120006}
ListViewSubitem: {R0120009}
etc.
R0120006 and R0120009 are the items in column 1
here is my code:
Private Sub ListView_ItemDrag(ByVal sender As Object, ByVal e As _
System.Windows.Forms.ItemD
ListView1.ItemDrag
Dim myItem As ListViewItem
Dim myItems(sender.SelectedIte
Dim i As Integer = 0
' Loop though the SelectedItems collection for the source.
For Each myItem In sender.SelectedItems
' Add the ListViewItem to the array of ListViewItems.
myItems(i) = myItem
i = i + 1
Next
' Create a DataObject containg the array of ListViewItems.
sender.DoDragDrop(New _
DataObject("System.Windows
DragDropEffects.Move)
End Sub
Private Sub ListView_DragEnter(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragE
' Check for the custom DataFormat ListViewItem array.
If e.Data.GetDataPresent("Sys
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub ListView_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragE
Dim myItem As ListViewItem
Dim myItems() As ListViewItem = e.Data.GetData("System.Win
Dim i As Integer = 0
For Each myItem In myItems
' Add the item to the target list.
Dim itmX As New ListViewItem
itmX.Text = myItems(i).Text
For ctr As Integer = 0 To myItems(i).SubItems.Count - 1
itmX.SubItems.Add(myItems(
Next ctr
sender.Items.Add(itmX)
' Remove the item from the source list.
If sender Is AccountsLV Then
ListView1.Items.Remove(Lis
Else
AccountsLV.Items.Remove(Ac
End If
i = i + 1
Next
End Sub
Hi I am really confused. Does the target listview have 2 columns?
Please do this:
itmX.Text = myItems(i).Text
Debug.WriteLine(itmX.Text)
For ctr As Integer = 0 To myItems(i).SubItems.Count - 1
itmX.SubItems.Add(myItems( i).SubItem s(ctr).ToS tring)
Debug.WriteLine(myItems(i) .SubItems( ctr).ToStr ing)
Next ctr
Now does the output in the immediate window do as expected? e.g you get the right data in the right columns?
Please do this:
itmX.Text = myItems(i).Text
Debug.WriteLine(itmX.Text)
For ctr As Integer = 0 To myItems(i).SubItems.Count - 1
itmX.SubItems.Add(myItems(
Debug.WriteLine(myItems(i)
Next ctr
Now does the output in the immediate window do as expected? e.g you get the right data in the right columns?
ASKER
I am still getting:
In Column 2, instead of adding the subitem text, it adds:
ListViewSubitem: {R0120006}
ListViewSubitem: {R0120009}
etc.
R0120006 and R0120009 are the items in column 1
In Column 2, instead of adding the subitem text, it adds:
ListViewSubitem: {R0120006}
ListViewSubitem: {R0120009}
etc.
R0120006 and R0120009 are the items in column 1
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
still doing it... I have no idea why
Can you post the code that you use to populate the listview items where they are being dragged from please?
ASKER
I am pulling the information from excel:
(oSheet is the variable I am using for the excel sheet)
Dim rowsA As Long = oSheet.Cells(oSheet.Rows.C ount, 2).End(xlUp).Row
Dim i As Long
For i = 1 To rowsA
Dim MyItem As New ListViewItem
MyItem = AccountsLV.Items.Add(oShee t.Cells(i, 2).Value, 0)
MyItem.SubItems.Add(oSheet .Cells(i, 3).Value)
Next
(oSheet is the variable I am using for the excel sheet)
Dim rowsA As Long = oSheet.Cells(oSheet.Rows.C
Dim i As Long
For i = 1 To rowsA
Dim MyItem As New ListViewItem
MyItem = AccountsLV.Items.Add(oShee
MyItem.SubItems.Add(oSheet
Next
Oh how stupid of me! DOH!
Sorry what the line in the listitem reference to the subitem should be [.Text]:
Private Sub ListView_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragE ventArgs) Handles AccountsLV.DragDrop, ListView1.DragDrop
Dim myItem As ListViewItem
Dim myItems() As ListViewItem = e.Data.GetData("System.Win dows.Forms .ListViewI tem()")
Dim i As Integer = 0
For Each myItem In myItems
' Add the item to the target list.
Dim itmX As New ListViewItem
itmX.Text = myItems(i).Text
For ctr As Integer = 0 To myItems(i).SubItems.Count - 1
itmX.SubItems.Add(myItems( i).SubItem s(ctr).Tex t)
Next ctr
sender.Items.Add(itmX)
' Remove the item from the source list.
If sender Is AccountsLV Then
ListView1.Items.Remove(Lis tView1.Sel ectedItems .Item(0))
Else
AccountsLV.Items.Remove(Ac countsLV.S electedIte ms.Item(0) )
End If
i = i + 1
Next
End Sub
Sorry what the line in the listitem reference to the subitem should be [.Text]:
Private Sub ListView_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragE
Dim myItem As ListViewItem
Dim myItems() As ListViewItem = e.Data.GetData("System.Win
Dim i As Integer = 0
For Each myItem In myItems
' Add the item to the target list.
Dim itmX As New ListViewItem
itmX.Text = myItems(i).Text
For ctr As Integer = 0 To myItems(i).SubItems.Count - 1
itmX.SubItems.Add(myItems(
Next ctr
sender.Items.Add(itmX)
' Remove the item from the source list.
If sender Is AccountsLV Then
ListView1.Items.Remove(Lis
Else
AccountsLV.Items.Remove(Ac
End If
i = i + 1
Next
End Sub
ASKER
This is a horrible cycle... now it is giving me the same value from column 1 in column 2:
R0120006 R0120006
R0120009 R0120009
Here is my code:
Private Sub ListView_ItemDrag(ByVal sender As Object, ByVal e As _
System.Windows.Forms.ItemD ragEventAr gs) Handles AccountsLV.ItemDrag, _
ListView1.ItemDrag
Dim myItem As ListViewItem
Dim myItems(sender.SelectedIte ms.Count - 1) As ListViewItem
Dim i As Integer = 0
' Loop though the SelectedItems collection for the source.
For Each myItem In sender.SelectedItems
' Add the ListViewItem to the array of ListViewItems.
myItems(i) = myItem
For ctr As Integer = 0 To myitem.subitems.count - 1
myItems(i).SubItems.Add(my Item.SubIt ems(ctr).T oString)
Next ctr
i = i + 1
Next
' Create a DataObject containg the array of ListViewItems.
sender.DoDragDrop(New _
DataObject("System.Windows .Forms.Lis tViewItem( )", myItems), _
DragDropEffects.Move)
End Sub
Private Sub ListView_DragEnter(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragE ventArgs) Handles AccountsLV.DragEnter, ListView1.DragEnter
' Check for the custom DataFormat ListViewItem array.
If e.Data.GetDataPresent("Sys tem.Window s.Forms.Li stViewItem ()") Then
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub ListView_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragE ventArgs) Handles AccountsLV.DragDrop, ListView1.DragDrop
Dim myItem As ListViewItem
Dim myItems() As ListViewItem = e.Data.GetData("System.Win dows.Forms .ListViewI tem()")
Dim i As Integer = 0
For Each myItem In myItems
' Add the item to the target list.
Dim itmX As New ListViewItem
itmX.Text = myItems(i).Text
For ctr As Integer = 0 To myItems(i).SubItems.Count - 1
itmX.SubItems.Add(myItems( i).SubItem s(ctr).Tex t)
Next ctr
sender.Items.Add(itmX)
' Remove the item from the source list.
If sender Is AccountsLV Then
ListView1.Items.Remove(Lis tView1.Sel ectedItems .Item(0))
Else
AccountsLV.Items.Remove(Ac countsLV.S electedIte ms.Item(0) )
End If
i = i + 1
Next
End Sub
R0120006 R0120006
R0120009 R0120009
Here is my code:
Private Sub ListView_ItemDrag(ByVal sender As Object, ByVal e As _
System.Windows.Forms.ItemD
ListView1.ItemDrag
Dim myItem As ListViewItem
Dim myItems(sender.SelectedIte
Dim i As Integer = 0
' Loop though the SelectedItems collection for the source.
For Each myItem In sender.SelectedItems
' Add the ListViewItem to the array of ListViewItems.
myItems(i) = myItem
For ctr As Integer = 0 To myitem.subitems.count - 1
myItems(i).SubItems.Add(my
Next ctr
i = i + 1
Next
' Create a DataObject containg the array of ListViewItems.
sender.DoDragDrop(New _
DataObject("System.Windows
DragDropEffects.Move)
End Sub
Private Sub ListView_DragEnter(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragE
' Check for the custom DataFormat ListViewItem array.
If e.Data.GetDataPresent("Sys
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub ListView_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragE
Dim myItem As ListViewItem
Dim myItems() As ListViewItem = e.Data.GetData("System.Win
Dim i As Integer = 0
For Each myItem In myItems
' Add the item to the target list.
Dim itmX As New ListViewItem
itmX.Text = myItems(i).Text
For ctr As Integer = 0 To myItems(i).SubItems.Count - 1
itmX.SubItems.Add(myItems(
Next ctr
sender.Items.Add(itmX)
' Remove the item from the source list.
If sender Is AccountsLV Then
ListView1.Items.Remove(Lis
Else
AccountsLV.Items.Remove(Ac
End If
i = i + 1
Next
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
IT WORKED! Thanks so much for your patience
Oh I just love horrible cycles, they make my day when you get out of them!