Link to home
Start Free TrialLog in
Avatar of handyjay
handyjay

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of handyjay
handyjay

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial