ComponentOne True DBGrid Pro 8
Tutorial 13 - Implementing Drag-and-Drop in True DBGrid

This tutorial demonstrates how you can use the drag-and-drop features of True DBGrid to drag data from one grid and drop it into another.

  1. Start a new project.

    Place two Data controls (Data1 and Data2), two True DBGrid controls (TDBGrid1 and TDBGGrid2), and two Labels (Label1 and Label2) on the form (Form1) as shown in this figure:

  2. Set the DatabaseName property of Data1 and Data2 to TDBGDemo.MDB, the RecordSource property of Data1 to Customers, and the RecordSource property of Data2 to CallList.

  3. Set the DataSource property of TDBGrid1 to Data1, and the DataSource property of TDBGrid2 to Data2.

  4. Set the DragIcon property of TDBGrid1 to ARW09LT.ICO, which can be found in the BITMAPS subdirectory of the True DBGrid installation directory, or in the ICONS\ARROWS subdirectory of the Visual Basic installation directory.

  5. Set the Visible property of Data2 to False.

  6. Set the Caption property of Label1 to "Drag from here:" and the Caption property of Label2 to "To here:".

    Configuring the grids at design time

    We shall configure TDBGrid1 and TDBGrid2 at design time using techniques described in previous tutorials. We will only briefly outline the steps below. If you are not familiar with the basic techniques, please refer to Tutorial 6 and Design Time Interaction before continuing.

  7. Right-click TDBGrid1 to display its context menu. Choose Retrieve Fields to configure TDBGrid1's layout to the RecordSet defined by the RecordSource property of Data1.

  8. Right-click TDBGrid1 again to display its context menu. This time choose Edit to enter the grid's visual editing mode.

  9. Use the visual editing menu to delete the following columns from TDBGrid1: UserCode, Contacted, and CustType. To delete a column, first select it by clicking its header, then right-click the grid to display the visual editing menu, then select Delete from the menu.

  10. Adjust the column widths of the grid (by dragging the column dividers in the column header area) so that all columns will fit within the grid's display area.

  11. Right-click the grid to display the visual editing menu and choose Properties to display the Property Pages dialog. On the Splits page, expand Splits(00) and set the MarqueeStyle property to 1 - Solid Cell Border. On the Columns page, expand Column3 (Phone) and select its NumberFormat node. Type (###)###-#### into the combo box labeled Selection or String. The grid's NumberFormat property provides the same functionality as Visual Basic's Format function.

  12. Repeat steps 8, 9, and 11 with TDBGrid2. Omit step 10 since we want to keep all three columns (Customer, Phone, and CallDate) in the grid.

  13. Right-click the grid to display the visual editing menu and choose Properties to display the Property Pages dialog. On the Splits page, expand Splits(00) and set the MarqueeStyle property to 1 - Solid Cell Border. On the Columns page, expand Column1 (Phone) and set the NumberFormat property to (###)###-#### as in step 12. Then expand Column2 (CallDate) and set the NumberFormat property to MM/DD HH:NNa/p.

  14. Click the OK button at the bottom of the Property Pages dialog to accept the changes. The grids on your form should now look like this.

    If you choose, you can also set the ExtendRightColumn property of both grids to True (on the Splits property page) to eliminate the gray area between the rightmost column and the grid's right border.

    Adding code to your project

    This section describes the code needed to drag the contents of a cell or row from TDBGrid1 to TDBGrid2. The code assumes that if you drag from the Phone column, you want to drag only the phone number data to another cell in TDBGrid2. If you drag from any other column, however, the code assumes that you want to drag the entire row of data to TDBGrid2 in order to add a new record there.

  15. Add the following subroutine to your project to reset the MarqueeStyle property of each grid, which is used to provide visual feedback while dragging is in progress. The reset routine will be called to perform clean-up after a drag-and-drop operation concludes.

    Private Sub ResetDragDrop()

    ' Turn off drag-and-drop by resetting the highlight and data control caption.

        If TDBGrid1.MarqueeStyle = dbgSolidCellBorder Then Exit Sub

        TDBGrid1.MarqueeStyle = dbgSolidCellBorder

        TDBGrid2.MarqueeStyle = dbgSolidCellBorder

        Data1.Caption = "Drag a row, or just phone #"

    End Sub

  16. The DragCell event is fired when dragging is initiated in a grid cell. The following code prepares for dragging data from the cell:

    Private Sub TDBGrid1_DragCell(ByVal SplitIndex As Integer, _

            RowBookmark As Variant, ByVal ColIndex As Integer)

        ' DragCell is triggered when True DBGrid detects an attempt to drag data from a cell.

     

        ' Set the current cell to the one being dragged.

        TDBGrid1.Col = ColIndex

        TDBGrid1.Bookmark = RowBookmark

     

        ' See if the starting cell is in the "Phone" column.

        Select Case TDBGrid1.Columns(ColIndex).Caption

            Case "Phone"

                ' Highlight the phone number cell to indicate data from the cell

                ' is being dragged.

                TDBGrid1.MarqueeStyle = dbgHighlightCell

                Data1.Caption = "Dragging phone number..."

            Case Else

                ' Highlight the entire row to indicate data from the entire row is

                ' being dragged.

                TDBGrid1.MarqueeStyle = dbgHighlightRow

                Data1.Caption = "Create new call when dropped..."

        End Select

     

        ' Use Visual Basic manual drag support.

        TDBGrid1.Drag vbBeginDrag

    End Sub

  17. The following code provides different visual feedback to the user when dragging over TDBGrid2:

    Private Sub TDBGrid2_DragOver(Source As Control, _

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

        ' DragOver provides different visual feedback as we are dragging a row, or just the phone number.

     

        Dim dragFrom As String

        Dim overCol As Integer

        Dim overRow As Long

     

        dragFrom = TDBGrid1.Columns(TDBGrid1.Col).DataField

     

        Select Case State

            Case vbEnter

                If dragFrom = "Phone" Then

                    TDBGrid2.MarqueeStyle = dbgHighlightCell

                Else

                    TDBGrid2.MarqueeStyle = dbgNoMarquee

                End If

            Case vbLeave

                TDBGrid2.MarqueeStyle = dbgHighlightCell

            Case vbOver

                If dragFrom = "Phone" Then

                    overCol = 1

                Else

                    overCol = TDBGrid2.ColContaining(X)

                End If

                overRow = TDBGrid2.RowContaining(Y)

                If overCol >= 0 Then TDBGrid2.Col = overCol

                If overRow >= 0 Then TDBGrid2.Row = overRow

        End Select

    End Sub

  18. When data is dropped on TDBGrid2, we either update the Phone column of TDBGrid2 or add a new record:

    Private Sub TDBGrid2_DragDrop(Source As Control, _

            X As Single, Y As Single)

        ' Allow phone drops right into the cell, other drops cause a new row to be added.

     

        If TDBGrid1.Columns(TDBGrid1.Col).Caption = "Phone" Then

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

                TDBGrid1.Columns(TDBGrid1.Col).Value

            Data2.UpdateRecord

        Else

            Data2.Recordset.AddNew

            Data2.Recordset!CallDate = Now

            Data2.Recordset!Phone = Data1.Recordset!Phone

            Data2.Recordset!Customer = Data1.Recordset!FirstName & _

                " " & Data1.Recordset!LastName & ", " & _

                Data1.Recordset!Company

            Data2.Recordset.Update

            Data2.Recordset.MoveLast

        End If

     

        ResetDragDrop

    End Sub

  19. The following code performs clean-up when the mouse returns to TDBGrid1 with the button up:

    Private Sub TDBGrid1_MouseMove(Button As Integer, _

            Shift As Integer, X As Single, Y As Single)

        ' If the button is up and we get MouseMove, that means we exited the form

        ' and tried to drop elsewhere. Reset the drag upon returning.

        If Button = 0 Then ResetDragDrop

    End Sub

Run the program and observe the following:

This concludes Tutorial 13.

 

 


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

Product Support Forum  |  Documentation Feedback