ComponentOne True DBGrid Pro 8
Drag-and-Drop Behavior

Typically, implementing a drag-and-drop interface using a grid is a painstaking task, since you normally want to drag data from a particular cell or row to another. Visual Basic's drag-and-drop facilities work with entire controls, but do not provide features for detecting which element of a control is involved.

True DBGrid has a special event, DragCell, designed to simplify the initiation of a drag operation. DragCell is called whenever the user attempts to drag data from a cell to another location; your code can respond accordingly. DragCell informs you of the split index, row bookmark, and column index of the cell being dragged. Typically, you will save the information so that it is available when the drag-and-drop operation terminates. At design time, be sure to change the grid's DragIcon (Visual Basic) property to a meaningful icon, since the default behavior is to drag an outline of the grid.

Note: The DragCell event will not fire for the current cell when the grid's MarqueeStyle property is set to the default value of 6 - Floating Editor. This is because the floating editor processes mouse events itself, as it must handle insertion point movement and text selection. For more information, see Highlighting the Current Row or Cell.

For example, assume that you want to be able to drag a row of data elsewhere. The following code in the DragCell event handler starts the drag-and-drop operation:

Example Title
Copy Code
Private Sub TDBGrid1_DragCell(ByVal SplitIndex As Integer, _

        RowBookmark As Variant, ByVal ColIndex As Integer)

 

' Set the current cell to the one being dragged.

TDBGrid1.Col = ColIndex

TDBGrid1.Bookmark = RowBookmark

 

' Set up drag operation, such as creating visual effects by  highlighting the cell

' or row being dragged.

 

' Use VB manual drag support (put TDBGrid1 into drag mode).

TDBGrid1.Drag vbBeginDrag

End Sub

 

Note that the Col and Bookmark properties of the grid are set to reflect the cell that was dragged. Once this event is completed, Visual Basic takes over the drag operation (see the Visual Basic documentation for the Drag method). Place code in the DragDrop event of the destination to perform the actions related to the drop.

If the destination of a drag operation is another True DBGrid control, and you want to drop data into a row or cell, you need to consider the following:

Often, the difficulty with implementing such operations on grids is that, given the mouse location, it is difficult to find out which cell, row, or column you are about to drop into. True DBGrid solves this problem by providing the SplitContaining, ColContaining, and RowContaining methods, which translate mouse coordinates into a grid location. You can also use the PointAt method to detect whether the drop position is over a data cell or a static element such as a caption bar or record selector.

Suppose that you want to provide feedback to the user about which cell is under the mouse pointer. The easiest way to do this is in the DragOver (Visual Basic) event, which fires as the mouse moves over the destination grid. Here's how you would set the current cell pointer so that it tracks the dragging object:

Example Title
Copy Code
Private Sub TDBGrid2_DragOver(Source As Control, _

        X As Single, Y As Single, State As Integer)

 

    ' Set current cell to "track" the dragging object.

    Dim overCol As Integer

    Dim overRow As Long

    overCol = TDBGrid2.ColContaining(X)

    overRow = TDBGrid2.RowContaining(Y)

    If overCol >= 0 Then TDBGrid2.Col = overCol

    If overRow >= 0 Then TDBGrid2.Row = overRow

End Sub

When the drop occurs [detected in the DragDrop (Visual Basic) event], you can move the appropriate data into the destination grid, or perform whatever action you want the drop to trigger. For example, you can copy the contents of the dragged cell (which was made current in the DragCell example presented earlier) to the current cell in the destination grid:

Example Title
Copy Code
Private Sub TDBGrid2_DragDrop(Source As Control, _

        X As Single, Y As Single)

TDBGrid2.Columns(TDBGrid2.Col).Value = _

    TDBGrid1.Columns(TDBGrid1.Col).Value

End Sub

You should also perform some clean-up when the drag-and-drop operation fails or the user completes the drop outside the boundaries of the destination control. Tutorial 13 demonstrates how to implement a drag-and-drop interface from one grid to another.

True DBGrid also supports OLE drag-and-drop as demonstrated in Tutorial 14.

 

 


Copyright (c) GrapeCity, inc. All rights reserved.

Product Support Forum  |  Documentation Feedback