Results 1 to 10 of 10

Thread: drag drop from datagrid to listview

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Posts
    471

    drag drop from datagrid to listview

    I know how to drag drop from listview to datagrid, but , how to do the other way? Thanks.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: drag drop from datagrid to listview

    The principles of drag and drop are the same no matter what you're doing. Handle the MouseDown event of the control you're dragging from, call the DoDragDrop method and pass the data you want to drag, handle the DragDrop event of the control you're dragging to, get the data and perfrom whatever operation is required.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Posts
    471

    Re: drag drop from datagrid to listview

    Yeah, that is the principles. Now I want to drag from grid to listview, I pass a selected row to listview, but I can not get that row in dragdrop event.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: drag drop from datagrid to listview

    Whatever you pass to the first argument of the DoDragDrop method in the MouseDown event handler is what you get back from the e.Data property in the DragDrop event. The following example is a little rough but it works. Assuming that the DataGrid is bound to a DataTable with columns named ID and Name, this will copy a row from the grid to the ListView when dragged:
    VB Code:
    1. Private Sub DataGrid1_MouseDown(ByVal sender As Object, _
    2.                                 ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseDown
    3.     Dim hti As DataGrid.HitTestInfo = Me.DataGrid1.HitTest(e.X, e.Y)
    4.  
    5.     If hti.Type = DataGrid.HitTestType.Cell Then
    6.         Me.DataGrid1.DoDragDrop(DirectCast(Me.DataGrid1.DataSource, DataTable).DefaultView(hti.Row), _
    7.                                 DragDropEffects.Copy)
    8.     End If
    9. End Sub
    10.  
    11. Private Sub ListView1_DragEnter(ByVal sender As Object, _
    12.                                 ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragEnter
    13.     If e.AllowedEffect = DragDropEffects.Copy Then
    14.         e.Effect = DragDropEffects.Copy
    15.     End If
    16. End Sub
    17.  
    18. Private Sub ListView1_DragDrop(ByVal sender As Object, _
    19.                                ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop
    20.     Dim row As DataRowView = DirectCast(e.Data.GetData(GetType(DataRowView)), DataRowView)
    21.     Dim item As New ListViewItem(row("ID").ToString())
    22.  
    23.     item.SubItems.Add(CStr(row("Name")))
    24.     Me.ListView1.Items.Add(item)
    25. End Sub
    Last edited by jmcilhinney; Oct 27th, 2006 at 06:43 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Posts
    471

    Re: drag drop from datagrid to listview

    Thanks so much!!! And I will need to do dragdrop between grids also. Let me try that first, and maybe I have to come back bug you again.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: drag drop from datagrid to listview

    Here's that same code rewritten slightly to illustrate what I was saying. The 'dataBeingDragged' variables in the MouseDown and DragDrop event handlers refer to the same object. The same data goes into the DodragDrop method and comes out of the e.Data property. As I said, it's exactly the same principle no matter waht controls are involved and no matter what data you're dragging. You just have to alter the way you get the data from e.Data for the format you want it in. Some data can be retrieved in more than one format, while others can't.
    VB Code:
    1. Private Sub DataGrid1_MouseDown(ByVal sender As Object, _
    2.                                 ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseDown
    3.     Dim hti As DataGrid.HitTestInfo = Me.DataGrid1.HitTest(e.X, e.Y)
    4.  
    5.     If hti.Type = DataGrid.HitTestType.Cell Then
    6.         'This is the data that will be returned by e.Data in the DragDrop event handler.
    7.         Dim dataBeingDragged As DataRowView = DirectCast(Me.DataGrid1.DataSource, DataTable).DefaultView(hti.Row)
    8.  
    9.         Me.DataGrid1.DoDragDrop(dataBeingDragged, DragDropEffects.Copy)
    10.     End If
    11. End Sub
    12.  
    13. Private Sub ListView1_DragEnter(ByVal sender As Object, _
    14.                                 ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragEnter
    15.     If e.AllowedEffect = DragDropEffects.Copy Then
    16.         e.Effect = DragDropEffects.Copy
    17.     End If
    18. End Sub
    19.  
    20. Private Sub ListView1_DragDrop(ByVal sender As Object, _
    21.                                ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop
    22.     'This is the data that was passed to the DoDragDrop method in the MouseDown event handler.
    23.     Dim dataBeingDragged As DataRowView = DirectCast(e.Data.GetData(GetType(DataRowView)), DataRowView)
    24.  
    25.     Dim item As New ListViewItem(dataBeingDragged("ID").ToString())
    26.  
    27.     item.SubItems.Add(CStr(dataBeingDragged("Name")))
    28.     Me.ListView1.Items.Add(item)
    29. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    New Member
    Join Date
    May 2008
    Location
    Where the buses don't run
    Posts
    2

    Re: drag drop from datagrid to listview

    I'd love to get this code to compile as I have been trying to implement drag and drop from a datagridview to a listview without success. The line of code with the error is the "hti" declaration in the "MouseDown" procedure. I get the following message when the mouse pointer hovers over this section of the code "Me.DataGrid1.HitTest(e.X, e.Y)":

    "Value of type 'System.Windows.Forms.DataGridView.HitTestInfo' cannot be converted to 'System.Windows.Forms.DataGrid.HitTestInfo'"

    DataGridView1 is bound to a DataTable and contains only two columns, one each for "cust_id" and "cust_name". I guess there must be something else that I've missed, probably something b-i-g...



    Private Sub DataGridView1_MouseDown(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown

    Dim hti As DataGrid.HitTestInfo = Me.DataGridView1.HitTest(e.X, e.Y)
    If hti.Type = DataGrid.HitTestType.Cell Then
    Dim dataBeingDragged As DataRowView = DirectCast(Me.DataGridView1.DataSource, DataTable).DefaultView(hti.Row)
    Me.DataGridView1.DoDragDrop(dataBeingDragged, DragDropEffects.Copy)
    End If
    End Sub


    Private Sub ListView1_DragEnter(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragEnter

    If e.AllowedEffect = DragDropEffects.Copy Then
    e.Effect = DragDropEffects.Copy
    End If
    End Sub


    Private Sub ListView1_DragDrop(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop

    Dim dataBeingDragged As DataRowView = DirectCast(e.Data.GetData(GetType(DataRowView)), DataRowView)
    Dim item As New ListViewItem(dataBeingDragged("cust_id").ToString())
    item.SubItems.Add(CStr(dataBeingDragged("cust_name")))
    Me.ListView1.Items.Add(item)
    End Sub

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: drag drop from datagrid to listview

    I would think the error message would speak for itself. The DataGrid and the DataGridView are two different controls, so they use two different HitTestInfo classes. You're using the wrong one.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    New Member
    Join Date
    May 2008
    Location
    Where the buses don't run
    Posts
    2

    Re: drag drop from datagrid to listview

    Well, that certainly is an eye-opener. I wasn't even aware that a "DataGrid" control existed. I had assumed - incorrectly, of course - that the term DataGrid was being used as shorthand for DataGridView. VB 2005 does not seem to have a DataGrid control available in the Toolbox (the DataGridView control is in the Toolbox, but no DataGrid control is available). Perhaps this might make it easier to understand how I've arrived at this point. Would you kindly direct me to the place where the DataGrid control might be found?

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: drag drop from datagrid to listview

    Quote Originally Posted by sea minkey
    Well, that certainly is an eye-opener. I wasn't even aware that a "DataGrid" control existed. I had assumed - incorrectly, of course - that the term DataGrid was being used as shorthand for DataGridView. VB 2005 does not seem to have a DataGrid control available in the Toolbox (the DataGridView control is in the Toolbox, but no DataGrid control is available). Perhaps this might make it easier to understand how I've arrived at this point. Would you kindly direct me to the place where the DataGrid control might be found?
    What i meant was that you're using the wrong HitTestInfo class, not the wrong control. You SHOULD be using the DataGridView so you SHOULD be using the DataGridView.HitTestInfo class. The problem is here:
    vb.net Code:
    1. Dim hti As DataGrid.HitTestInfo = Me.DataGridView1.HitTest(e.X, e.Y)
    The HitTest method of the DataGridView class returns a DataGridView.HitTestInfo object. As the error message says, that cannot be assigned to a DataGrid.HitTest variable. You need to declare your variable as the correct type.

    The DataGrid control has existed since the first version of .NET. In .NET 2.0 the DataGridView control was introduced as a replacement. It is superior in almost every way. The DataGrid is not in the Toolbox by default because you're not intended to use it. It still exists mainly for backward-compatibility, although if you do want to use it for some reason you can simply add it to the toolbox yourself, as you can with any component.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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