?
Solved

Drag and Drop between two listviews (in 'Details' view)

Posted on 2007-10-02
22
Medium Priority
?
673 Views
Last Modified: 2013-11-27
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.ItemDragEventArgs) Handles AccountsLV.ItemDrag, _
    AssetsLV.ItemDrag
        Dim myItem As ListViewItem
        Dim myItems(sender.SelectedItems.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.ListViewItem()", myItems), _
        DragDropEffects.Move)
    End Sub

    Private Sub ListView_DragEnter(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.DragEventArgs) Handles AccountsLV.DragEnter, AssetsLV.DragEnter
        ' Check for the custom DataFormat ListViewItem array.
        If e.Data.GetDataPresent("System.Windows.Forms.ListViewItem()") 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.DragEventArgs) Handles AccountsLV.DragDrop, AssetsLV.DragDrop
        Dim myItem As ListViewItem
        Dim myItems() As ListViewItem = e.Data.GetData("System.Windows.Forms.ListViewItem()")
        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(AssetsLV.SelectedItems.Item(0))
            Else
                AccountsLV.Items.Remove(AccountsLV.SelectedItems.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(ByVal 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?
0
Comment
Question by:tiehaze
  • 11
  • 10
22 Comments
 
LVL 21

Expert Comment

by:surajguptha
ID: 20003386
Trying to understand, you are able to drag and drop using the other views but not able to do the same with Details View?
0
 

Author Comment

by:tiehaze
ID: 20006827
Yes, correct
0
 
LVL 5

Expert Comment

by:RainUK
ID: 20019940
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?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:tiehaze
ID: 20022300
It works, but all it is transferring is column 1.... I need both columns to transfer
0
 
LVL 5

Expert Comment

by:RainUK
ID: 20022402
You need to iterate the subitems e.g.

Private Sub ListView_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles AccountsLV.DragDrop, AssetsLV.DragDrop
        Dim myItem As ListViewItem
        Dim myItems() As ListViewItem = e.Data.GetData("System.Windows.Forms.ListViewItem()")
        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).subitems(ctr).tostring              

        next ctr

            sender.Items.Add(itmX)

            ' Remove the item from the source list.
            If sender Is AccountsLV Then
                AssetsLV.Items.Remove(AssetsLV.SelectedItems.Item(0))
            Else
                AccountsLV.Items.Remove(AccountsLV.SelectedItems.Item(0))
            End If
            i = i + 1

        Next
    End Sub
0
 

Author Comment

by:tiehaze
ID: 20022533
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?
0
 
LVL 5

Expert Comment

by:RainUK
ID: 20022566
sorry the line should be

itmX.Subitems.add(myItems(i).subitems(ctr)

(Without the ToString)
0
 

Author Comment

by:tiehaze
ID: 20022620
Now it is just repeating the items that are in column 1 in column 2:

R0120006   R0120006
R0120009   R0120009
0
 
LVL 5

Expert Comment

by:RainUK
ID: 20022673
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).subitems(ctr).tostring)              

        next ctr

YOU DO NEED the ToString [My mistake, not reading the code)
0
 
LVL 5

Expert Comment

by:RainUK
ID: 20022691
Paste your code here again please
0
 

Author Comment

by:tiehaze
ID: 20022829
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.ItemDragEventArgs) Handles AccountsLV.ItemDrag, _
    ListView1.ItemDrag
        Dim myItem As ListViewItem
        Dim myItems(sender.SelectedItems.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.ListViewItem()", myItems), _
        DragDropEffects.Move)
    End Sub

    Private Sub ListView_DragEnter(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.DragEventArgs) Handles AccountsLV.DragEnter, ListView1.DragEnter
        ' Check for the custom DataFormat ListViewItem array.
        If e.Data.GetDataPresent("System.Windows.Forms.ListViewItem()") 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.DragEventArgs) Handles AccountsLV.DragDrop, ListView1.DragDrop
        Dim myItem As ListViewItem
        Dim myItems() As ListViewItem = e.Data.GetData("System.Windows.Forms.ListViewItem()")
        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).SubItems(ctr).ToString)
            Next ctr
            sender.Items.Add(itmX)
            ' Remove the item from the source list.
            If sender Is AccountsLV Then
                ListView1.Items.Remove(ListView1.SelectedItems.Item(0))
            Else
                AccountsLV.Items.Remove(AccountsLV.SelectedItems.Item(0))
            End If
            i = i + 1
        Next
    End Sub
0
 
LVL 5

Expert Comment

by:RainUK
ID: 20022927
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).SubItems(ctr).ToString)

               Debug.WriteLine(myItems(i).SubItems(ctr).ToString)

            Next ctr

Now does the output in the immediate window do as expected? e.g you get the right data in the right columns?
0
 

Author Comment

by:tiehaze
ID: 20023118
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
0
 
LVL 5

Assisted Solution

by:RainUK
RainUK earned 2000 total points
ID: 20023180
I can only think that the copy of the listview item in the item drag event does not contain the subitems collection for each item. Hence you can try to write a debug statement to check or alter:

Private Sub ListView_ItemDrag(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.ItemDragEventArgs) Handles AccountsLV.ItemDrag, _
    ListView1.ItemDrag
        Dim myItem As ListViewItem
        Dim myItems(sender.SelectedItems.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(myitem.Subitems(ctr).ToString)

            next ctr

            i = i + 1
        Next
        ' Create a DataObject containg the array of ListViewItems.
        sender.DoDragDrop(New _
        DataObject("System.Windows.Forms.ListViewItem()", myItems), _
        DragDropEffects.Move)
    End Sub
0
 

Author Comment

by:tiehaze
ID: 20023335
still doing it... I have no idea why
0
 
LVL 5

Expert Comment

by:RainUK
ID: 20023385
Can you post the code that you use to populate the listview items where they are being dragged from please?
0
 

Author Comment

by:tiehaze
ID: 20024264
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.Count, 2).End(xlUp).Row
        Dim i As Long
        For i = 1 To rowsA
            Dim MyItem As New ListViewItem
            MyItem = AccountsLV.Items.Add(oSheet.Cells(i, 2).Value, 0)
            MyItem.SubItems.Add(oSheet.Cells(i, 3).Value)
        Next
0
 
LVL 5

Expert Comment

by:RainUK
ID: 20024372
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.DragEventArgs) Handles AccountsLV.DragDrop, ListView1.DragDrop
        Dim myItem As ListViewItem
        Dim myItems() As ListViewItem = e.Data.GetData("System.Windows.Forms.ListViewItem()")
        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).SubItems(ctr).Text)
            Next ctr
            sender.Items.Add(itmX)
            ' Remove the item from the source list.
            If sender Is AccountsLV Then
                ListView1.Items.Remove(ListView1.SelectedItems.Item(0))
            Else
                AccountsLV.Items.Remove(AccountsLV.SelectedItems.Item(0))
            End If
            i = i + 1
        Next
    End Sub
0
 

Author Comment

by:tiehaze
ID: 20024409
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.ItemDragEventArgs) Handles AccountsLV.ItemDrag, _
        ListView1.ItemDrag
        Dim myItem As ListViewItem
        Dim myItems(sender.SelectedItems.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(myItem.SubItems(ctr).ToString)
            Next ctr
            i = i + 1
        Next
        ' Create a DataObject containg the array of ListViewItems.
        sender.DoDragDrop(New _
        DataObject("System.Windows.Forms.ListViewItem()", myItems), _
        DragDropEffects.Move)
    End Sub

    Private Sub ListView_DragEnter(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.DragEventArgs) Handles AccountsLV.DragEnter, ListView1.DragEnter
        ' Check for the custom DataFormat ListViewItem array.
        If e.Data.GetDataPresent("System.Windows.Forms.ListViewItem()") 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.DragEventArgs) Handles AccountsLV.DragDrop, ListView1.DragDrop
        Dim myItem As ListViewItem
        Dim myItems() As ListViewItem = e.Data.GetData("System.Windows.Forms.ListViewItem()")
        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).SubItems(ctr).Text)
            Next ctr
            sender.Items.Add(itmX)
            ' Remove the item from the source list.
            If sender Is AccountsLV Then
                ListView1.Items.Remove(ListView1.SelectedItems.Item(0))
            Else
                AccountsLV.Items.Remove(AccountsLV.SelectedItems.Item(0))
            End If
            i = i + 1
        Next
    End Sub
0
 
LVL 5

Accepted Solution

by:
RainUK earned 2000 total points
ID: 20024521
Offset the ctr by 1, it includes the .Text in the listviewitem as the first subitem for some reason.

e.g. For ctr As Integer = 1 To myItems(i).SubItems.Count - 1

So change it to :

Private Sub ListView_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles AccountsLV.DragDrop, ListView1.DragDrop
        Dim myItem As ListViewItem
        Dim myItems() As ListViewItem = e.Data.GetData("System.Windows.Forms.ListViewItem()")
        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 = 1 To myItems(i).SubItems.Count - 1
                itmX.SubItems.Add(myItems(i).SubItems(ctr).Text)
            Next ctr
            sender.Items.Add(itmX)
            ' Remove the item from the source list.
            If sender Is AccountsLV Then
                ListView1.Items.Remove(ListView1.SelectedItems.Item(0))
            Else
                AccountsLV.Items.Remove(AccountsLV.SelectedItems.Item(0))
            End If
            i = i + 1
        Next
    End Sub
0
 

Author Comment

by:tiehaze
ID: 20024543
IT WORKED! Thanks so much for your patience
0
 
LVL 5

Expert Comment

by:RainUK
ID: 20024587
Oh I just love horrible cycles, they make my day when you get out of them!
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question