Results 1 to 2 of 2

Thread: Drag-drop problem with listview

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Drag-drop problem with listview

    I am trying to move an entire row from one listview to another.
    Here is the code for the events:

    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         'ListView1.Items.Add("whatever", 0)
    3.         Dim constr As String = "connstr"
    4.         Dim conn As New SqlConnection(constr)
    5.         Dim strSql As String = "SELECT part_no_1, part_no_2, part_no_3, internal_order_no_pv, " & _
    6.             "qty_per_operation, remaining_qty FROM gpstest " & _
    7.             "WHERE operation_priority > 16 and operation_finished IS NULL " & _
    8.             "ORDER BY requ_deliv_day_op"
    9.         conn.Open()
    10.         Dim dr As SqlDataReader
    11.         Dim cmd As New SqlCommand(strSql, conn)
    12.         dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    13.         ListView1.Columns.Add("Col1", 40, HorizontalAlignment.Center)
    14.         ListView1.Columns.Add("Col2", 60, HorizontalAlignment.Center)
    15.         ListView2.Columns.Add("Col1", 40, HorizontalAlignment.Center)
    16.         ListView2.Columns.Add("Col2", 60, HorizontalAlignment.Center)
    17.         While dr.Read
    18.             Dim li As New ListViewItem(CStr(dr.Item(0)))
    19.             li.SubItems.Add(CStr(dr.Item(1)))
    20.             ListView1.Items.Add(li)
    21.             li = Nothing
    22.         End While
    23.         conn = Nothing
    24.         cmd = Nothing
    25.     End Sub
    26.  
    27.     Private Sub ListView1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseMove, ListView2.MouseMove
    28.         'First I check if a button has been pressed
    29.         If e.Button = MouseButtons.None Then
    30.             Exit Sub
    31.         End If
    32.  
    33.         'Second, I need to cast the sender object as a listview object
    34.         Dim lstView As ListView
    35.         lstView = DirectCast(sender, ListView)
    36.  
    37.         'Make sure at least one listview item is selected in the list
    38.  
    39.         If lstView.SelectedItems.Count = 0 Then
    40.             Exit Sub
    41.         End If
    42.  
    43.         'Create a variable to hold the item
    44.         Dim lstItem As ListViewItem = lstView.SelectedItems(0)
    45.  
    46.         Dim dob As New DataObject
    47.        
    48.         dob.SetData(DataFormats.Text, True, lstItem.Text)
    49.  
    50.         'At this point, our code wait here until the dragdrop is complete
    51.         Dim effect As DragDropEffects = DragDropEffects.Copy Or DragDropEffects.Move
    52.  
    53.         effect = lstView.DoDragDrop(dob, effect)
    54.  
    55.         'if successful, remove the item from the original listview
    56.         If effect = DragDropEffects.Move Then
    57.             lstView.Items.Remove(lstItem)
    58.         End If
    59.  
    60.     End Sub
    61.  
    62.     Private Sub ListView1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragEnter, ListView2.DragEnter
    63.         'We need to make sure the cursor changes when dragging an object
    64.         'Check if there is data present
    65.         If e.Data.GetDataPresent(DataFormats.Text, True) Then
    66.             e.Effect = e.AllowedEffect And DragDropEffects.Move
    67.         Else
    68.             e.Effect = DragDropEffects.None
    69.         End If
    70.  
    71.     End Sub
    72.  
    73.     Private Sub ListView1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop, ListView2.DragDrop
    74.         'Make sure there is some data in the dataobject clipboard before continue
    75.         If Not e.Data.GetDataPresent(DataFormats.Text, True) Then
    76.             Exit Sub
    77.         End If
    78.  
    79.         e.Effect = DragDropEffects.Move
    80.  
    81.         Dim lstView As ListView
    82.         lstView = DirectCast(sender, ListView)
    83.  
    84.         lstView.Items.Add(e.Data.GetData(DataFormats.Text, True), 0)
    85.     End Sub
    86. End Class

    It works ok for the item in the first column, but how do I move an entire row??? I am not fully confident in understanding the object model of the listview. Should I work with a listviewitemcollection instead??

    Thank you for the help
    Henrik
    Last edited by MrNorth; Nov 25th, 2003 at 07:54 AM.

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Instead of moving the Item text in DataFormat.Text move the whole item as an object. Like this:
    VB Code:
    1. Private Sub ListView1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseMove, ListView2.MouseMove
    2.         'First I check if a button has been pressed
    3.         If e.Button = MouseButtons.None Then
    4.             Exit Sub
    5.         End If
    6.  
    7.         'Second, I need to cast the sender object as a listview object
    8.         Dim lstView As ListView= DirectCast(sender, ListView)
    9.  
    10.         'Make sure at least one listview item is selected in the list
    11.  
    12.         If lstView.SelectedItems.Count = 0 Then
    13.             Exit Sub
    14.         End If
    15.  
    16.         'Create a variable to hold the item
    17.         Dim lstItem As ListViewItem = lstView.SelectedItems(0)
    18.  
    19.         Dim dob As New DataObject
    20.  
    21.         dob.SetData(lstItem.Clone) 'must clone or it gives an error
    22.  
    23.         'At this point, our code wait here until the dragdrop is complete
    24.         Dim effect As DragDropEffects = DragDropEffects.Copy Or DragDropEffects.Move
    25.  
    26.         effect = lstView.DoDragDrop(dob, effect)
    27.  
    28.         'if successful, remove the item from the original listview
    29.         If effect = DragDropEffects.Move Then
    30.             lstView.Items.Remove(lstItem)
    31.         End If
    32.  
    33.     End Sub
    34.  
    35.     Private Sub ListView1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragEnter, ListView2.DragEnter
    36.         'We need to make sure the cursor changes when dragging an object
    37.         'Check if there is data present
    38.         'If e.Data.GetDataPresent(DataFormats.Text, True) Then
    39.         If e.Data.GetDataPresent(GetType(ListViewItem)) Then
    40.             e.Effect = e.AllowedEffect And DragDropEffects.Move
    41.         Else
    42.             e.Effect = DragDropEffects.None
    43.         End If
    44.  
    45.     End Sub
    46.  
    47.     Private Sub ListView1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop, ListView2.DragDrop
    48.         'Make sure there is some data in the dataobject clipboard before continue
    49.         'If Not e.Data.GetDataPresent(DataFormats.Text, True) Then
    50.         If Not e.Data.GetDataPresent(GetType(ListViewItem)) Then
    51.             Exit Sub
    52.         End If
    53.  
    54.         e.Effect = DragDropEffects.Move
    55.  
    56.         Dim lstView As ListView = DirectCast(sender, ListView)
    57.         lstView.Items.Add(CType(e.Data.GetData(GetType(ListViewItem)), ListViewItem))
    58.     End Sub
    Last edited by Edneeis; Nov 25th, 2003 at 07:18 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width