• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 677
  • Last Modified:

Windows Forms Drag Drop and DataGridView Selected Row

I have 2 DataGridViews next to each other.  When I hover the mouse over a row I want to make it selected and higlighted.  

I have done this in the left grid before I start the Drag Drop operation.  Then when I click the mouse on the row I start a DRAGDROP operation and drag the Selected Row from the left grid over the right grid.  

 I then want the 2nd grid (right one) to  select the row that my mouse is hovering over so I know when to drop my selection.

See my snippet attached.  Is there a different way to do this that I am not trying?  
#Region "DRAG DROP"

 
    Private Sub dgvnew_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvnew.MouseDown
        Dim Index As Integer
        Index = dgvnew.HitTest(e.X, e.Y).RowIndex

        Dim uid As Integer = dgvnew.Rows(Index).Cells(1).Value

        If Index > -1 Then

            'Pass the Index as "Data" argument of the DoDragDrop Function         
            dgvnew.DoDragDrop(Index, DragDropEffects.Move)

        End If
    End Sub

    Private Sub dgvCurrentStore_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles dgvCurrentStore.DragDrop
        Try

            'Get the Index value that we stored in the Data in the MouseDown event          
            'In case we stored a PrimaryKey value here in place of Index, we would get that by Type casting it to its type           
            Dim index As Integer = Convert.ToInt32(e.Data.GetData(System.Type.GetType("System.Int32")))

            'Now based on the Index get the data in the Cells          
            'Again if we had Primary Key value here we would have used the underlying DataTable DT1 to get the data for that key          



            ' If MessageBox.Show(String.Format("Do you want to Attach {0} boxes of {1} with MaterialNumber {2} to this job?", nBoxes.ToString, nCategory, cellmaterialnumber.ToString), "Confirm Assignment", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
            MsgBox("YES")
            ' Else
            ' MsgBox("NO")
            ' End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub


    Private Sub dgvCurrentStore_DragOver(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles dgvCurrentStore.DragOver
        e.Effect = DragDropEffects.Move

    
    End Sub

    'slect the row the mouse is over by moving the mouse and hover
    Private Sub dgvnew_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvnew.MouseMove

        Dim Index As Integer
        Index = dgvnew.HitTest(e.X, e.Y).RowIndex
        If Index > -1 Then


            dgvnew.Rows(Index).Selected = True

        End If

    End Sub



    Private Sub dgvCurrentStore_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles dgvCurrentStore.DragEnter

        Dim Index As Integer
        Index = dgvCurrentStore.HitTest(e.X, e.Y).RowIndex

        If Index > -1 Then

            dgvCurrentStore.Rows(Index).Selected = True

        End If

    End Sub
    
#End Region

Open in new window

0
handyjay
Asked:
handyjay
1 Solution
 
handyjayAuthor Commented:
I figured this out myself.. ended up using a new thread and invoking a delegate.

heres a solution


    Delegate Sub ChangeSelectedRowOnDragDrop()
    Private myThread As Thread
    Public myDelegate As ChangeSelectedRowOnDragDrop

    Private Sub ThreadFunction()
        Dim myThreadClassObject As New MyThreadClass(Me)
        myThreadClassObject.Run()
    End Sub 'ThreadFunction

    Public Event dgvCurrentStoreMouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

    Public Sub ChangeSelectedRowOnDragDropMethod()
        ' The mouse locations are relative to the screen, so they must be
        ' converted to client coordinates.
        Dim clientPoint As Point = dgvCurrentStore.PointToClient(New Point(MousePosition.X, MousePosition.Y))

        ' Get the row index of the item the mouse is below.
        Dim index As Integer = dgvCurrentStore.HitTest(clientPoint.X, clientPoint.Y).RowIndex
        If index > -1 Then
            dgvCurrentStore.Rows(index).Selected = True

        End If
    End Sub
#End Region

    Private Sub dgvCurrentStore_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles dgvCurrentStore.DragEnter
        myDelegate = New ChangeSelectedRowOnDragDrop(AddressOf ChangeSelectedRowOnDragDropMethod)
        myThread = New Thread(New ThreadStart(AddressOf ThreadFunction))
        myThread.Start()
    End Sub
End Class

Public Class MyThreadClass
    Private myFormControl1 As FulfillmentInventory

    Public Sub New(ByVal myForm As FulfillmentInventory)
        myFormControl1 = myForm
    End Sub 'New

    Public Sub Run()
        ' Execute the specified delegate on the thread that owns
        ' 'myFormControl1' control's underlying window handle.
        myFormControl1.Invoke(myFormControl1.myDelegate)
    End Sub 'Run

End Class 'MyThreadClass
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now