dcsimg
Results 1 to 19 of 19

Thread: Drag and drop bound datagridview

  1. #1

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,081

    Drag and drop bound datagridview

    Hi,

    I have in the database one-to-many tables:

    tblContext ---> tblRiskAssessment

    I want to drag and drop rows in the tblRiskAssessment and then also save that back to the database.

    In tblRiskAssessment I have (among others):

    - RiskID
    - ContextID
    - RiskNumber
    - Event

    So If I move:

    RiskNumber: 10
    Event: Server failure

    to the top of the datagridview then it must look like:


    RiskNumber: 1
    Event: Server failure

    (RiskNumber 10 is now risk Number 1)

    I have tried this and get this error (on the red part below):

    Column 'RiskID' is read only
    Can someone please share how to drag and drop a bound datagridview, re-number and save?

    Thanks...


    Code:
    Public Class Form2
    
    
        Private dragBoxFromMouseDown As Rectangle
        Private rowIndexFromMouseDown As Integer
        Private rowIndexOfItemUnderMouseToDrop As Integer
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Me.TblRiskAssessmentTableAdapter.Fill(Me.RiskDBDataSet.tblRiskAssessment)
        End Sub
    
        Private Sub TblRiskAssessmentDataGridView_CellMouseMove(sender As Object, e As DataGridViewCellMouseEventArgs) Handles TblRiskAssessmentDataGridView.CellMouseMove
            If (e.Button And MouseButtons.Left) = MouseButtons.Left Then
                If dragBoxFromMouseDown <> Rectangle.Empty AndAlso (Not dragBoxFromMouseDown.Contains(e.X, e.Y)) Then
                    Dim dropEffect As DragDropEffects = TblRiskAssessmentDataGridView.DoDragDrop(TblRiskAssessmentDataGridView.Rows(rowIndexFromMouseDown), DragDropEffects.Move)
                End If
            End If
        End Sub
    
        Private Sub TblRiskAssessmentDataGridView_MouseDown(sender As Object, e As MouseEventArgs) Handles TblRiskAssessmentDataGridView.MouseDown
            rowIndexFromMouseDown = TblRiskAssessmentDataGridView.HitTest(e.X, e.Y).RowIndex
            If rowIndexFromMouseDown <> -1 Then
                Dim dragSize As Size = SystemInformation.DragSize
                dragBoxFromMouseDown = New Rectangle(New Point(e.X - (dragSize.Width \ 2), e.Y - (dragSize.Height \ 2)), dragSize)
            Else
                dragBoxFromMouseDown = Rectangle.Empty
            End If
        End Sub
    
        Private Sub TblRiskAssessmentDataGridView_DragOver(sender As Object, e As DragEventArgs) Handles TblRiskAssessmentDataGridView.DragOver
            e.Effect = DragDropEffects.Move
        End Sub
    
        Private Sub TblRiskAssessmentDataGridView_DragDrop(sender As Object, e As DragEventArgs) Handles TblRiskAssessmentDataGridView.DragDrop
            Dim clientPoint As Point = TblRiskAssessmentDataGridView.PointToClient(New Point(e.X, e.Y))
            rowIndexOfItemUnderMouseToDrop = TblRiskAssessmentDataGridView.HitTest(clientPoint.X, clientPoint.Y).RowIndex
    
            If e.Effect = DragDropEffects.Move Then
                Dim rowToMove As DataGridViewRow = TryCast(e.Data.GetData(GetType(DataGridViewRow)), DataGridViewRow)
    
                Dim celldata(rowToMove.Cells.Count - 1) As Object
                For col As Integer = 0 To rowToMove.Cells.Count - 1
                    celldata(col) = rowToMove.Cells(col).Value
                Next
    
    
                Dim row As DataRow = DirectCast(TblRiskAssessmentBindingSource.Current, DataRowView).Row
    
                row.ItemArray = celldata
                TblRiskAssessmentBindingSource.DataTable.Rows.InsertAt(row, rowIndexOfItemUnderMouseToDrop)
    
                rowToMove.DataGridView.Rows.Remove(rowToMove)
    
                TblRiskAssessmentDataGridView.CurrentCell = TblRiskAssessmentDataGridView(0, rowIndexOfItemUnderMouseToDrop)
            End If
        End Sub
    
    End Class
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,753

    Re: Drag and drop bound datagridview

    The logical thing to do is to sort the data by a specific column and then simply change the values in that column. The data will rearrange itself. That column doesn't have to be anything to do with the database if a suitable one doesn't exist. You can add an extra column to the underlying DataTable specifically for the purpose.

  3. #3

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,081

    Re: Drag and drop bound datagridview

    You did get that the question is about drag and drop a datagridview row that is bound?
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,753

    Re: Drag and drop bound datagridview

    Quote Originally Posted by schoemr View Post
    You did get that the question is about drag and drop a datagridview row that is bound?
    I did. By dragging and dropping you are changing the order in which the rows are displayed in the grid. You can also change the order in which the rows are displayed in the grid by sorting them appropriately. What exactly is to be achieved by changing their order in the underlying DataTable?

  5. #5

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,081

    Re: Drag and drop bound datagridview

    Quote Originally Posted by jmcilhinney View Post
    I did. By dragging and dropping you are changing the order in which the rows are displayed in the grid. You can also change the order in which the rows are displayed in the grid by sorting them appropriately. What exactly is to be achieved by changing their order in the underlying DataTable?
    John, It is not only to change the 'display' (how it is presented to the user)

    I have (as in #1)

    - RiskID
    - ContextID
    - RiskNumber
    - Event

    Say I have 3 Events and every event have a number. After the assessment a report is print. In that report will be every event and the number of that event. (the Risk number will always start at "1" to n and the ID'a are hidden.

    E.g.

    1 - Hardware fail
    2 - Power outage
    3 - Fire


    But in the database it may look like below (with RiskID and ContextID respectively - which I cannot change as the ID's are auto generated. The records can be deleted and added all the time so the RiskID my not always follow each other, but all RiskID's do belong to the same ContextID:


    RiskID - ContextID - RiskNumber - Event

    23 - 12 - 1 - Hardware fail
    46 - 12 - 2 - Power outage
    80 - 12 - 3 - Fire


    Now the user wants to move Fire to the number two spot by means of drag and drop: (obviously in the real scenario it it is much more - it can be e.g. drag event number 150 to 20, or 5 to 90)

    23 - 12 - 1 - Hardware fail
    80 - 12 - 2 - Fire
    46 - 12 - 3 - Power outage

    Fire was event number 3, after the drag en drop it is event number 2...
    Last edited by schoemr; Aug 22nd, 2019 at 04:00 AM.
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,753

    Re: Drag and drop bound datagridview

    So then order the data by RiskNumber and then, as I said, change the values in that RiskNumber column. Moving the rows around yourself is pointless. That's not going to change any data. You'd still have to change the data anyway so why not only change the data and let the rows order themselves?

  7. #7

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,081

    Re: Drag and drop bound datagridview

    Without drag and drop?
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,753

    Re: Drag and drop bound datagridview

    What I'm talking about is what you do when the user drops. It is completely up to you what you do when the user drops a row that they just dragged. Removing that row and inserting it is pointless when you can simply change some data in it and have it move itself.

  9. #9

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,081

    Re: Drag and drop bound datagridview

    It is completely up to you what you do when the user drops a row that they just dragged
    Okay

    So, according to you I can still drag and drop the row. It would then mean the following.

    When the row is drop -

    1 - I only change one column in that row (RiskNumber)
    2 - I need to know 'where' that row is dropped in order to give RiskNumber the correct number
    3 - ID's will not change (it don't have to)
    4 - Re-number the entire RiskNumber column

    for number 4 it is because say I have:

    1
    2
    3
    4
    ..
    ..
    ..
    ..
    300
    301
    302

    and I drag 300 to number 3 spot:

    it will change to: (note there is two no 3's now, and no more a 300)

    1
    2
    3
    3
    4
    ..
    ..
    ..
    ..

    301
    302

    If you can please be so kind to confirm then I shall do some research on how to do it...
    '
    Ps. Thank you very much John I understand now (I think). I did not have to change the ID in the first place....

    The example I have only shows me how to do that for the entire row........

    I need to find only the RiskNumber column.
    Last edited by schoemr; Aug 22nd, 2019 at 06:31 AM.
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,753

    Re: Drag and drop bound datagridview

    Yeah, that's pretty much what I had in mind.

  11. #11

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,081

    Re: Drag and drop bound datagridview

    You would not maybe have an example of how to drag and drop a single column? I can't find anything. All I see is for an entire row....

    Code:
    Private Sub dataGridView1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
        dataGridView1.DoDragDrop(dataGridView1.SelectedRows, DragDropEffects.Move)
    End Sub
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  12. #12

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,081

    Re: Drag and drop bound datagridview

    Here is an entire (working) example. But it is for the entire row..... I somehow need to modify this only for a single column in that row.......?


    Code:
    Public Class Form2
    
        Dim dtSource As DataTable
        Dim SourceRowIndex As Integer = -1
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            dtSource = New DataTable("SourceTable")
            With dtSource
                For j As Integer = 0 To 5
                    .Columns.Add("Col" & j.ToString)
                Next j
                For i As Integer = 0 To 9
                    Dim sArr(5) As String
                    For j As Integer = 0 To 5
                        sArr(j) = String.Format("{0}, {1}", i.ToString, j.ToString)
                    Next j
                    .Rows.Add(sArr)
                Next i
            End With
            dgvTest.DataSource = dtSource
        End Sub
    
        Private Sub Form2_Disposed(sender As Object, e As EventArgs) Handles Me.Disposed
            If Not IsNothing(dtSource) Then
                dtSource.Clear()
                dtSource.Dispose()
            End If
        End Sub
        Private Sub DGVTest_MouseDown(sender As Object, e As MouseEventArgs) Handles DGVTest.MouseDown
            SourceRowIndex = -1
            If e.Button = Windows.Forms.MouseButtons.Left Then
                SourceRowIndex = DGVTest.HitTest(e.X, e.Y).RowIndex
                DGVTest.Rows(SourceRowIndex).Selected = True
                DGVTest.Refresh()
                DoDragDrop(DGVTest.Rows(SourceRowIndex), DragDropEffects.Move)
            End If
        End Sub
    
        Private Sub DGVTest_DragOver(sender As Object, e As DragEventArgs) Handles DGVTest.DragOver
            If SourceRowIndex >= 0 Then
                e.Effect = DragDropEffects.Move
            End If
        End Sub
    
        Private Sub DGVTest_DragDrop(sender As Object, e As DragEventArgs) Handles DGVTest.DragDrop
            Dim pt As Point = DGVTest.PointToClient(New Point(e.X, e.Y))
            Dim TargetRowIndex As Integer = DGVTest.HitTest(pt.X, pt.Y).RowIndex
            Dim drMove As DataRow = dtSource.NewRow
            drMove.ItemArray = dtSource.Rows(SourceRowIndex).ItemArray
            dtSource.Rows.RemoveAt(SourceRowIndex)
            dtSource.Rows.InsertAt(drMove, TargetRowIndex)
        End Sub
    
        Private Sub DGVTest_MouseUp(sender As Object, e As MouseEventArgs) Handles DGVTest.MouseUp
            SourceRowIndex = -1
        End Sub
    End Class
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,431

    Re: Drag and drop bound datagridview

    If you drag the value from a single column, what happens to the rest of the columns in the row? do they just remain with the now empty cell? Also where are you planning on dropping it? into an empty cell in the same column?

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,431

    Re: Drag and drop bound datagridview

    Assuming you intend to dragdrop from column x to another row in column x, i'd create a class...

    Code:
    Public Class dropObject
        Public value as Object
        Public columnToDropTo as integer
    End Class
    value is the cell value
    columnToDropTo is the column you want to allow as a dropTarget

    Code:
    Public Class Form2
    
        Dim dtSource As DataTable
        Dim SourceRowIndex As Integer = -1
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            dtSource = New DataTable("SourceTable")
            With dtSource
                For j As Integer = 0 To 5
                    .Columns.Add("Col" & j.ToString)
                Next j
                For i As Integer = 0 To 9
                    Dim sArr(5) As String
                    For j As Integer = 0 To 5
                        sArr(j) = String.Format("{0}, {1}", i.ToString, j.ToString)
                    Next j
                    .Rows.Add(sArr)
                Next i
            End With
            dgvTest.DataSource = dtSource
        End Sub
    
        Private Sub Form2_Disposed(sender As Object, e As EventArgs) Handles Me.Disposed
            If Not IsNothing(dtSource) Then
                dtSource.Clear()
                dtSource.Dispose()
            End If
        End Sub
        Private Sub DGVTest_MouseDown(sender As Object, e As MouseEventArgs) Handles DGVTest.MouseDown
            Dim columnIndex as integer = DGVTest.HitTest(e.X, e.Y).ColumnIndex
            If columnIndex = aColumnYouAllowToBeDragged then
                SourceRowIndex = -1
                If e.Button = Windows.Forms.MouseButtons.Left Then
                    SourceRowIndex = DGVTest.HitTest(e.X, e.Y).RowIndex
                    DGVTest.Rows(SourceRowIndex).Cells(columnIndex).Selected = True
                    DGVTest.Refresh()
                    DoDragDrop(new dropObject With {.value = DGVTest.Rows(SourceRowIndex).Cells(columnIndex).value, .columnToDropTo = columnIndex}, DragDropEffects.Move)
                    DGVTest.Rows(SourceRowIndex).Cells(columnIndex).value = ""
                End If
            End If
        End Sub
    
        Private Sub DGVTest_DragOver(sender As Object, e As DragEventArgs) Handles DGVTest.DragOver
            If SourceRowIndex >= 0 Then
                e.Effect = DragDropEffects.Move
            End If
        End Sub
    
        Private Sub DGVTest_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles DGVTest.DragDrop
            Dim pt As Point = DGVTest.PointToClient(New Point(e.X, e.Y))
            Dim TargetRowIndex As Integer = DGVTest.HitTest(pt.X, pt.Y).RowIndex
            Dim TargetColumnIndex As Integer = DGVTest.HitTest(pt.X, pt.Y).ColumnIndex
            Dim o As dropObject = DirectCast(e.Data.GetData(GetType(dropObject)), dropObject)
            If o.columnToDropTo = TargetColumnIndex Then
                DGVTest.Rows(TargetRowIndex).Cells(TargetColumnIndex).value = o.value
            End If
        End Sub
    
        Private Sub DGVTest_MouseUp(sender As Object, e As MouseEventArgs) Handles DGVTest.MouseUp
            SourceRowIndex = -1
        End Sub
    End Class

  15. #15

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,081

    Re: Drag and drop bound datagridview

    Quote Originally Posted by .paul. View Post
    If you drag the value from a single column, what happens to the rest of the columns in the row? do they just remain with the now empty cell? Also where are you planning on dropping it? into an empty cell in the same column?
    Hello Paul,

    Thank you very much for your answer.

    I am sorry for my delay in reply. I am still working through your code.

    To answer your question, I want to move the entire row (with all its columns) from one position in DGVx to another position in the same DGVx.

    So... How do I do this currently. Say I have 4 rows: (the number is the RiskNumber)

    1 - aa
    2 - bb
    3 - cc
    4 - dd

    The BindingSource is sorted by RiskNumber. So... Say I want to move "cc" to the top I manually change the riskNumber to:

    1 - cc

    Then it automatically moves to the top and then it looks like this:

    1 - cc
    1 - aa
    2 - bb
    4 - dd

    But now there is two "1"'s... I have to renumber. I use the Index:

    Code:
    For Each row As DataGridViewRow In TblRiskAssessmentDataGridView.Rows
    row.Cells(0).Value = row.Index + 1
      Next

    Then it looks like this:

    1 - cc
    2 - aa
    3 - bb
    4 - dd


    And this I want to do with drag and dop... I only have to change 1 column value (RiskNumber)
    Last edited by schoemr; Sep 4th, 2019 at 02:50 AM.
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  16. #16

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,081

    Re: Drag and drop bound datagridview

    Paul,

    Please see what happens....

    This is my dgv:

    Name:  1.JPG
Views: 17
Size:  10.2 KB

    Now, say I want to move "ddd" then this happens:

    Name:  2.JPG
Views: 18
Size:  10.0 KB

    It removes it... Also if i click on the other rows it removes it...
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  17. #17

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,081

    Re: Drag and drop bound datagridview

    Paul,

    Please see what happens....

    This is my dgv:

    Name:  1.JPG
Views: 17
Size:  10.2 KB

    Now, say I want to move "ddd" then this happens:

    Name:  2.JPG
Views: 18
Size:  10.0 KB

    It removes it... Also if i click on the other rows it removes it...
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  18. #18

    Thread Starter
    Frenzied Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    1,081

    Re: Drag and drop bound datagridview

    If I keep the mouse button down I can move it.. It is working.. But it removes the "target" value...

    So say I move "ddd" to the top..

    Then after the drop it looks like this:

    Name:  1.JPG
Views: 18
Size:  10.1 KB

    "ddd" did in fact move to the top, but the "aaa" that was there before is gone now and in the number 4 position (where "ddd" was before) there is nothing now...
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  19. #19
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    144

    Re: Drag and drop bound datagridview

    This might be a very wrong placed question, but .....

    I would have thought the datasources of the grids, should also be updated.
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width