Solved

drag and drop from a datagridview to a list box

Posted on 2014-01-31
6
520 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 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