Results 1 to 4 of 4

Thread: Drag and Drop on a DataGridView Bound to a Filtered BindingSource

Threaded View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2010
    Posts
    64

    Unhappy Drag and Drop on a DataGridView Bound to a Filtered BindingSource

    Help please - this is driving me nuts.
    I am trying to implement Drag and Drop Functionality on a Datagridview that is bound to a Filtered BindingSource (Me.QuoteLinesBindingSource.Filter = "QuoteID = " & id) and the data table has an AutoIncrement ID.
    I have not specified a sortorder for the Grid...so it appears to display in the ID order.

    By Googling I found some code - but it doesnt seem to work for a Filtered List?
    So I have modified it to try looking for the row in my Bindingsource.
    When running ... my row moves (mostly but not always to the correct place)
    But after Saving the data (QuoteLinesBindingSource.EndEdit() and QuoteLinesTableAdapter.Update(HEATPRODataSet1.QuoteLines))
    I can see that the row I tried to move is still where it was....and a copy of the row shows as the last grid row.

    I suspect the AutoIncrement may be the cause of at least part of my problem - as the moved row is added to the bottom of the Database Table - I am considering removing the ID as it is not really used.

    My code is as folows..


    Code:
    Dim dragBoxFromMouseDown As Rectangle
    Dim rowIndexFromMouseDown, rowIndexOfItemUnderMouseToDrop As Integer
    
    Me.QuoteLinesDataGridView.AllowDrop = True ' Enable Drag and Drop
    
    Private Sub QuoteLinesDataGridView_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles QuoteLinesDataGridView.DragDrop
        Try
            Dim clientPoint As Point = QuoteLinesDataGridView.PointToClient(New Point(e.X, e.Y))
            rowIndexOfItemUnderMouseToDrop = QuoteLinesDataGridView.HitTest(clientPoint.X, clientPoint.Y).RowIndex
            If e.Effect = DragDropEffects.Move Then
                Dim GridrowToMove As DataGridViewRow = CType(e.Data.GetData(GetType(DataGridViewRow)), DataGridViewRow)
                Dim DataRowToMove As DataRow = CType(GridrowToMove.DataBoundItem, DataRowView).Row
                Dim row As DataRow = HEATPRODataSet1.QuoteLines.NewRow()
                row.ItemArray = DataRowToMove.ItemArray
                HEATPRODataSet1.QuoteLines.Rows.Remove(DataRowToMove) ' remove old row - Changes on Grid ok - But not in Database after Save
                HEATPRODataSet1.QuoteLines.Rows.InsertAt(row, rowIndexOfItemUnderMouseToDrop) ' insert new row
                '****Also tried the following and got the same results****
               '****HEATPRODataSet1.Tables("QuoteLines").Rows.Remove(DataRowToMove)
               '****HEATPRODataSet1.Tables("QuoteLines").Rows.InsertAt(row, rowIndexOfItemUnderMouseToDrop)
            End If
        Catch ex As System.Exception
            MessageBox.Show("Drag and Drop Error" & vbCrLf & ex.ToString)
        End Try
    End Sub
    
    Private Sub QuoteLinesDataGridView_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles QuoteLinesDataGridView.DragOver
        e.Effect = DragDropEffects.Move
    End Sub
    
    Private Sub QuoteLinesDataGridView_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles QuoteLinesDataGridView.MouseDown
        rowIndexFromMouseDown = QuoteLinesDataGridView.HitTest(e.X, e.Y).RowIndex
        If rowIndexFromMouseDown <> -1 Then
            Dim dragSize As Size = SystemInformation.DragSize
            dragBoxFromMouseDown = New Rectangle(New Point(CInt(e.X - (dragSize.Width / 2)), CInt(e.Y - (dragSize.Height / 2))), dragSize)
        Else
            dragBoxFromMouseDown = Rectangle.Empty
        End If
    End Sub
    
    Private Sub QuoteLinesDataGridView_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles QuoteLinesDataGridView.MouseMove
        If e.Button = Windows.Forms.MouseButtons.Left Then
            If dragBoxFromMouseDown <> Rectangle.Empty And dragBoxFromMouseDown.Contains(e.X, e.Y) Then
                Dim dropEffect As DragDropEffects = QuoteLinesDataGridView.DoDragDrop(QuoteLinesDataGridView.Rows(rowIndexFromMouseDown), DragDropEffects.Move)
            End If
        End If
    End Sub
    Last edited by Anthonynz; Apr 26th, 2018 at 06:03 AM. Reason: corrected heading

Tags for this Thread

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