James Looi

James Looi

  • NA
  • 2
  • 4.1k

How to Drag drop data from datagridview to a listbox?

Jan 17 2012 8:26 PM
May I know how to drag and drop selected item form data grid view to list box? Below code didn't work..may I know where goes wrong?

Private Sub dtgModule_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dtgModule.MouseDown
           
If (e.Button = MouseButtons.Right) Then
           
Dim info As DataGridView.HitTestInfo = dtgModule.HitTest(e.X, e.Y)
           
If (info.RowIndex >= 0) Then
               
Dim view As DataRowView = CType(dtgModule.Rows(info.RowIndex).DataBoundItem, DataRowView)
               
If (Not (view) Is Nothing) Then
                    dtgModule
.DoDragDrop(view, DragDropEffects.Copy)
               
End If
           
End If
       
End If
   
End Sub


   
Private Sub ListBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragEnter
         e
.Effect = DragDropEffects.Copy
       
End Sub


Private Sub ListBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragDrop
     
If e.Data.GetDataPresent(GetType(Custom)) Then
           
' Determine which category the item was draged to
           
Dim p As Point = ListBox1.PointToClient(New Point(e.X, e.Y))
           
Dim index As Integer = ListBox1.IndexFromPoint(p)
            MsgBox
("index")
           
If (index = -1) Then
                index
= (ListBox1.Items.Count - 1)
           
End If
           
Dim objects As Custom = CType(e.Data.GetData(GetType(DataRowView)), Custom)
           
Dim newID As Integer = Integer.Parse(ListBox1.Items(index).ToString)
           
Dim oldID As Integer = objects.ID
           
If (oldID <> newID) Then
                 objects
.ID = newID
               
End If
       
End If
End Sub