Solved

drag and drop from a datagridview to a list box

Posted on 2014-01-31
6
512 Views
Last Modified: 2014-02-11
i had this working, and then it stopped ...

not sure what i did.

object is to  grab a row from a datagridview and put it into whichever list box i end up over.

DGV has 3 columns, i am only putting column 1 and 0 in the list box.

now if i try and grab a row and move it.. the datagridview1_mousedown fires  but when i move the mouse, i get a circle with a slash and when i move it over the lists box the dragenter doesnt fire
when i let go of the mosue the circle slash dissappears

i have allow drop set to true for all the listboxes.






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

        DataGridView1.DoDragDrop(DataGridView1.SelectedRows, DragDropEffects.Move)

    End Sub





    Private Sub ListBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragDrop
        Dim rows As DataGridViewSelectedRowCollection = DirectCast(e.Data.GetData(GetType(DataGridViewSelectedRowCollection)), DataGridViewSelectedRowCollection)
        Dim Row_String As String
        For Each row As DataGridViewRow In rows
            Row_String = RTrim(LTrim(row.Cells(1).Value.ToString)) + " " + row.Cells(0).Value
            ListBox1.Items.Add(Row_String)
            DataGridView1.Rows.Remove(row)
        Next

    End Sub

    Private Sub ListBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)

        If (e.Data.GetDataPresent(GetType(DataGridViewSelectedRowCollection))) Then
            e.Effect = DragDropEffects.Move
        End If


    End Sub


    Private Sub ListBox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
        Dim rows As DataGridViewSelectedRowCollection = DirectCast(e.Data.GetData(GetType(DataGridViewSelectedRowCollection)), DataGridViewSelectedRowCollection)
        Dim Row_String As String

        For Each row As DataGridViewRow In rows
            Row_String = RTrim(LTrim(row.Cells(1).Value.ToString)) + " " + row.Cells(0).Value
            ListBox2.Items.Add(Row_String)
            DataGridView1.Rows.Remove(row)
        Next

    End Sub

    Private Sub ListBox2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)

        If (e.Data.GetDataPresent(GetType(DataGridViewSelectedRowCollection))) Then
            e.Effect = DragDropEffects.Move
        End If


    End Sub
0
Comment
Question by:CASorter
  • 3
  • 2
6 Comments
 
LVL 25

Expert Comment

by:SStory
ID: 39830668
First, I suggest on DragEnter, do a messagebox of the type data to be sure that it is what you think it is.
0
 

Author Comment

by:CASorter
ID: 39832867
I have a stop in the dragenter....   it does not hit it

the circle with the line appears right after I start to move the mouse (after I left click), and stays until I let go of the mouse button.  but no dragenter events fire on any of the listboxes.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39833025
Instead of DataGridView1_MouseDown, move the code to MouseMove
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 25

Accepted Solution

by:
SStory earned 500 total points
ID: 39835441
Be sure the event handlers have the Handles clause at the right side--sometimes those disappear and the event never fires because of it.

Here's an example from Microsoft MSDN (source: http://msdn.microsoft.com/en-us/library/aa289508%28v=vs.71%29.aspx)

Private MouseIsDown As Boolean = False

Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
    ' Set a flag to show that the mouse is down.
    MouseIsDown = True
End Sub

Private Sub TextBox1_MouseMove(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseMove
    If MouseIsDown Then
        ' Initiate dragging.
        TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy)
    End If
    MouseIsDown = False
End Sub

Private Sub TextBox2_DragEnter(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles TextBox2.DragEnter
    ' Check the format of the data being dropped.
    If (e.Data.GetDataPresent(DataFormats.Text)) Then
        ' Display the copy cursor.
        e.Effect = DragDropEffects.Copy
    Else
        ' Display the no-drop cursor.
        e.Effect = DragDropEffects.None
    End If
End Sub

Private Sub TextBox2_DragDrop(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
    ' Paste the text.
    TextBox2.Text = e.Data.GetData(DataFormats.Text)
End Sub

Open in new window

0
 

Author Comment

by:CASorter
ID: 39843342
have been withouth power for 3 days.. will try this on monday
0
 

Author Closing Comment

by:CASorter
ID: 39850554
that worked...  

thanks
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now