locked
Datagridview - drag and drop operation RRS feed

  • Question

  • Hello,

    I am trying to build a application that build a tower with all kind of different blocks, and I made a little test that does the trick in my opinion.

    It currently adds cylinders and cone pieces into a Binding-list. This binding-list is bound to a data-grid that show the properties of my blocks that are added.

    Now, I would like to implement something to help the user correct or alter his initial tower. This would be swapping positions of the blocks, by dragging and dropping the position.

    Is this possible while using an bound object? If yes, how do you go start the drag-drop operation, I had some experience using drag and drop but not in the datagrid context.


    • Edited by E_Jef Wednesday, March 16, 2016 11:06 AM corrected title
    Wednesday, March 16, 2016 10:55 AM

Answers

  • And how does one do this?

    This is what you are currently doing: "It currently adds cylinders and cone pieces into a Binding-list. This binding-list is bound to a data-grid that show the properties of my blocks that are added."  You have a debug statement for identifying the row number for the drop operation, and you have said that the selected row is deleted.  So your concern appears to be with the bound source rather than the drag drop.

    You need to remove an item from its current position in this binding list, and insert it at its new position in the list.  That process will be similar to what you did when you created this list in its original sequence. You probably need to inform the binding source that an update is required. See:
    https://msdn.microsoft.com/en-us/library/ms132702(v=vs.110).aspx


    Wednesday, March 16, 2016 8:40 PM

All replies

  • The DataGrid (and in your case DataGridView) is not a spreadsheet. Moreover I think you use windows forms tabpage so this is not even a datagrid. 

    For your challenge you need a grid, probably the best to use with WPF.


    Success
    Cor


    Wednesday, March 16, 2016 11:00 AM
  • The DataGrid is not a spreadsheet. Moreover I think you use windows forms tabpage so this is not even a datagrid. 

    For your challenge you need a grid, probably the best to use with WPF.


    Success
    Cor

    Why is this not a datagrid? The object is called datagridview?
    Wednesday, March 16, 2016 11:07 AM
  • Now, I would like to implement something to help the user correct or alter his initial tower. This would be swapping positions of the blocks, by dragging and dropping the position.

    Is your concern about using drag and drop in a DGV, or using drag and drop with a bound data source?

    If you handle both the drag and the drop operations within one control then there should be no particular difficulty.   You will need to determine the exact item to be dragged and the exact location at which it gets dropped, but that applies to any drag and drop operation.

    The fact that the datagridview is bound only means that you update the data source for the result of the drag drop operation, not the DGV itself.

    Wednesday, March 16, 2016 11:08 AM
  • The DataGrid is not a spreadsheet. Moreover I think you use windows forms tabpage so this is not even a datagrid. 

    For your challenge you need a grid, probably the best to use with WPF.


    Success
    Cor

    Why is this not a datagrid? The object is called datagridview?
    You corrected the title so you are just a cheater. Bye


    Cor


    Wednesday, March 16, 2016 11:15 AM
  • The DataGrid is not a spreadsheet. Moreover I think you use windows forms tabpage so this is not even a datagrid. 

    For your challenge you need a grid, probably the best to use with WPF.


    Success
    Cor


    I corrected it, as you mentioned it wasn't 100% correct.. I don't know the difference between a data-grid or a data-grid view.

    Wednesday, March 16, 2016 11:25 AM
  • The DataGrid is not a spreadsheet. Moreover I think you use windows forms tabpage so this is not even a datagrid. 

    For your challenge you need a grid, probably the best to use with WPF.


    Success
    Cor

    Why is this not a datagrid? The object is called datagridview?

    You corrected the title so you are just a cheater. Bye


    Cor


    Cor, I swear that 3/4 of the reason I visit this forum is to read your replies. lol

    Live as if you were going to die today, learn as if you were going to live forever.

    Wednesday, March 16, 2016 11:46 AM
  • Is your concern about using drag and drop in a DGV, or using drag and drop with a bound data source?

    If you handle both the drag and the drop operations within one control then there should be no particular difficulty.   You will need to determine the exact item to be dragged and the exact location at which it gets dropped, but that applies to any drag and drop operation.

    The fact that the datagridview is bound only means that you update the data source for the result of the drag drop operation, not the DGV itself.

    My concern is using drag and drop with a bound source.

    I tried this, but it's not adding my row back at the desired position. I guess this is due to the use of my bound source? The deleting of the row does affect my bound source... So I only need to get it back in at the right position.

        Public oRowIndexMouseDown As Integer
        Public oRow As DataGridViewRow
    
        Private Sub BodyAssemblyDrag_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
            Handles DataGridView1.MouseDown
            If DataGridView1.SelectedRows.Count = 1 Then
                If e.Button = MouseButtons.Left Then
                    oRow = DataGridView1.SelectedRows(0)
                    oRowIndexMouseDown = DataGridView1.SelectedRows(0).Index
                    Debug.Print("Row to move = " & oRowIndexMouseDown)
    
                    DataGridView1.DoDragDrop(sender, DragDropEffects.Move)
                End If
            End If
        End Sub
    
        Private Sub BodyAssemblyDrag_dragenter(ByVal sender As Object, ByVal e As DragEventArgs) Handles DataGridView1.DragEnter
            If DataGridView1.SelectedRows.Count = 1 Then
                e.Effect = DragDropEffects.Move
            End If
        End Sub
    
        Private Sub BodyAssemblyDrag_dragdrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles DataGridView1.DragDrop
            Dim oPoint As Point
            oPoint = DataGridView1.PointToClient(New Point(e.X, e.Y))
    
            Dim oRowIndexMouseDrop As Integer
            oRowIndexMouseDrop = DataGridView1.HitTest(oPoint.X, oPoint.Y).RowIndex
    
            Debug.Print("Drop row @ " & oRowIndexMouseDrop)
    
            If Not oRowIndexMouseDrop = oRowIndexMouseDown Then
                DataGridView1.Rows.RemoveAt(oRowIndexMouseDown)
                DataGridView1.Rows.Insert(oRowIndexMouseDrop, oRow)
            End If
        End Sub

    Wednesday, March 16, 2016 12:27 PM
  • I am thinking you need to do remove/inserts to the datatable, and not the dgv, as previsouly suggested.

    Live as if you were going to die today, learn as if you were going to live forever.

    Wednesday, March 16, 2016 12:58 PM
  • I am thinking you need to do remove/inserts to the datatable, and not the dgv, as previsouly suggested.

    Live as if you were going to die today, learn as if you were going to live forever.

    And how does one do this?
    Wednesday, March 16, 2016 3:21 PM

  • And how does one do this?
    Can you put the code you use for binding?

    Live as if you were going to die today, learn as if you were going to live forever.

    Wednesday, March 16, 2016 4:10 PM
  • And how does one do this?

    This is what you are currently doing: "It currently adds cylinders and cone pieces into a Binding-list. This binding-list is bound to a data-grid that show the properties of my blocks that are added."  You have a debug statement for identifying the row number for the drop operation, and you have said that the selected row is deleted.  So your concern appears to be with the bound source rather than the drag drop.

    You need to remove an item from its current position in this binding list, and insert it at its new position in the list.  That process will be similar to what you did when you created this list in its original sequence. You probably need to inform the binding source that an update is required. See:
    https://msdn.microsoft.com/en-us/library/ms132702(v=vs.110).aspx


    Wednesday, March 16, 2016 8:40 PM