10620 : Drag in grid

Question

Can you tell me if it is possible to implement drag and drop of rows in the grid area?  I can’t see any built-in support but any advice would be much appreciated, we need to be able to drag rows within a “sibling” array and also between “parents”

Answer

You can do drag & drop. In the general download look for a sample called Gantt_GridDragDrop.

You need to start the drag in OnMouseDown, and decide what to do in OnDragDrop…


		Private Sub gantt1_OnGridMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
		  If Not gantt1.Grid.GridStructure.FocusedCell Is Nothing Then
			gantt1.Grid.StopRowResize()
			  gantt1.DoDragDrop(gantt1.Grid.GridStructure.FocusedCell.Content.Value.ToString(), DragDropEffects.All)
		  End If
		End Sub

		Private Sub gantt1_OnGridDragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
		  ' Drag drop events return screen coordinates. 
		  ' You must convert it to a Client coordinate before using it in GTP.NET
		  Dim localpoint As Point=gantt1.Grid.PointToClient(New Point(e.X,e.Y))
		  Dim cell As Cell=gantt1.Grid.GridStructure.CellFromXY(localpoint.X,localpoint.Y)
		  If Not cell Is Nothing AndAlso Not cell.Node.ParentNode Is Nothing AndAlso Not gantt1.Grid.GridStructure.FocusedCell Is Nothing Then
				gantt1.Grid.GridStructure.FocusedCell.Node.MoveNode(cell.Node.ParentNode,cell.Node.Index)
		  End If
		End Sub

		Private Sub gantt1_OnGridDragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
			If e.Data.GetDataPresent(DataFormats.Text) Then
				e.Effect = DragDropEffects.Move
			End If
		End Sub

		Private Sub gantt1_OnGridDragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)

			If e.Data.GetDataPresent(DataFormats.Text) Then
				e.Effect = DragDropEffects.Move
			Else
				e.Effect = DragDropEffects.None
			End If
		End Sub

       	Private Sub PrepareEvents()

	  AddHandler gantt1.OnGridDragDrop, AddressOf gantt1_OnGridDragDrop
	  AddHandler gantt1.OnGridDragEnter, AddressOf gantt1_OnGridDragEnter
	  AddHandler gantt1.OnGridDragOver, AddressOf gantt1_OnGridDragOver
	  AddHandler gantt1.OnGridMouseDown, AddressOf gantt1_OnGridMouseDown

	End Sub


Leave a Reply