ComponentOne True DBGrid Pro 8
Tutorial 4 - Interacting with Code and Other Bound Controls

In this tutorial, you will learn how True DBGrid interacts with other bound controls and with Visual Basic code that manipulates the same Recordset to which the grid is bound.

  1. Start a new project.

  2. Place the following controls on the form (Form1) as shown in the figure: a Data control (Data1), a True DBGrid control (TDBGrid1), a DBList control (DBList1), three text controls (Text1 to 3), seven command buttons (Command1 to 7), and four labels (Label1 to 4).

  3. Set the DatabaseName property of Data1 to TDBGDemo.MDB, and the RecordSource property to Customer.

  4. Set the DataSource property of TDBGrid1 to Data1, and the AllowAddNew and AllowDelete properties to True.

  5. Set the DataSource and RowSource control properties of DBList1 to Data1, and the ListField control, DataField, and BoundColumn control properties to LastName.

  6. Set the DataSource properties of Text1, Text2 and Text3 to Data1, and the DataField properties to FirstName, LastName, and Company, respectively.

  7. Set the Caption properties of Label1-Label4 and Command1-Command7 as shown in the preceding figure.

    We will be using code to affect the record position and data of the database. The seven buttons on this form will contain all of the code we use in this tutorial (the bound controls require no code).

  8. Add the following code to the Update button, Command1:

    Example Title
    Copy Code
    Private Sub Command1_Click()
    
    ' True DBGrid will automatically respond to the update and will clear the
    
    ' "modified indicator" (the pencil icon) on the record selector column to
    
    ' indicate that the modified data has been written to the database.
    
     
    
        Data1.Recordset.Edit
    
        Data1.Recordset.Update
    
        TDBGrid1.SetFocus
    
    End Sub
    

    This button triggers an immediate update of all modified data in the bound controls (the grid and the three text controls) without moving the current row position.

  9. Add the following code to the Delete button, Command2:

    Example Title
    Copy Code
    Private Sub Command2_Click()
    
    ' When the current record is deleted, Jet Engine leaves the record pointer at
    
    ' the deleted record. Use MoveNext to move the current record to the row after
    
    ' the deleted record.
    
     
    
        Data1.Recordset.Delete
    
        Data1.Recordset.MoveNext
    
       
    
        ' If the last record is deleted, move the current record to the last record.
    
       
    
        If Data1.Recordset.EOF = True Then
    
            Data1.Recordset.MoveLast
    
        End If
    
       
    
        TDBGrid1.SetFocus
    
    End Sub
    

    When the current record is deleted from code, the Data control leaves the record pointer at the deleted record. Therefore, the preceding code uses the MoveNext method of the Recordset to move the current record to the row after the deleted record.

  10. Add the following code to the AddNew button, Command3:

    Example Title
    Copy Code
    Private Sub Command3_Click()
    
    ' This "Add New" button moves the cursor to the "new (blank) row" at the end
    
    ' so that user can start adding data to the new record.
    
      
    
        ' Move to last record so that "new row" will be visible.
    
        Data1.Recordset.MoveLast
    
       
    
        ' Move the cursor to the "addnew row".
    
        TDBGrid1.Row = TDBGrid1.Row + 1
    
        TDBGrid1.SetFocus
    
    End Sub
    

    The above code demonstrates how to move the current cell to the new (blank) row at the end so that the user can start adding data to the new record.

  11. Add the following code to the First button, Command4:

    Example Title
    Copy Code
    Private Sub Command4_Click()
    
    ' True DBGrid will follow the record movement.
    
        Data1.Recordset.MoveFirst
    
        TDBGrid1.SetFocus
    
    End Sub
    

    This button positions the record pointer to the first record in the Recordset.

  12. Add the following code to the Next button, Command5:

    Example Title
    Copy Code
    Private Sub Command5_Click()
    
    ' True DBGrid will follow the record movement.
    
     
    
        Data1.Recordset.MoveNext
    
       
    
        ' Keep the record away from EOF which is not a valid position.
    
        If Data1.Recordset.EOF = True Then
    
            Data1.Recordset.MovePrevious
    
        End If
    
       
    
        TDBGrid1.SetFocus
    
    End Sub
    

    This button moves the current row to the next record.

  13. Add the following code to the Previous button, Command6:

    Example Title
    Copy Code
    Private Sub Command6_Click()
    
    ' True DBGrid will follow the record movement.
    
     
    
        Data1.Recordset.MovePrevious
    
       
    
        ' Keep the record away from BOF which is not  a valid position.
    
        If Data1.Recordset.BOF = True Then
    
            Data1.Recordset.MoveNext
    
        End If
    
          
    
        TDBGrid1.SetFocus
    
    End Sub
    

    This button moves the current row to the previous record.

  14. Add the following code to the Last button, Command7:

    Example Title
    Copy Code
    Private Sub Command7_Click()
    
    ' True DBGrid will follow the record movement.
    
     
    
        Data1.Recordset.MoveLast
    
        TDBGrid1.SetFocus
    
    End Sub
    

    This button positions the record pointer to the last record in the Recordset.

Run the program and observe the following:

This concludes Tutorial 4.

 

 


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

Product Support Forum  |  Documentation Feedback