ComponentOne True DataControl 8.0
Tutorial 13 - One-to-One Links and Memory-Based Controls

Previous tutorials described the use of memory-based TData controls; that is, those with their DataMode property set to 1 - MemoryArray. This tutorial presents a more elaborate example of memory data sources, explaining how to fill memory arrays with data prior to presenting them to the user. The OpenData event is typically used to assign a pre-initialized array to a TData control.

Additionally, this tutorial demonstrates master-detail relationships and one-to-one links in the context of memory-based TData controls. The techniques used with memory-based controls work in exactly the same general way as they do with database data sources.

In this tutorial, we create an application with two grids: one of them showing a list of countries, and the other showing a list of cities that lie within the country currently selected in the first grid. The cities grid also displays the country name, which is related to each city record by means of a one-to-one link.

  1. Start a new project.

  2. Place the following controls on the form (Form1) as shown in the figure: three TData controls (TData1 to 3) and two DataGrid controls (DataGrid1, DataGrid2).

  3. Set properties as follows:

    Example Title
    Copy Code
    DataGrid1.DataSource TData1
    
    DataGrid1.AllowAddNew      True
    
    DataGrid2.DataSource TData2
    
    DataGrid2.AllowAddNew      True
    
  4. Set the DataMode property of TData1 to 1 - MemoryArray and define two fields: CountryNo and Country, with Data Type set to 2 - Integer and 8 - String, respectively. Follow these steps:

    Open the True DataControl property pages for TData1. On the DataSource property page select 1 - MemoryArray in the DataMode combo box. Go to the Fields page. For each of the two fields, create a new field by clicking the right mouse button and choosing New Field from the context menu. Change the default FIELD_0 name to the appropriate field name by typing it in the Name text box. Select the appropriate type in the Data Type combo box. Press OK to apply the changes.

  5. Set the DataMode property of TData2 to 1 - MemoryArray and define three fields with the following Name and Data Type settings: CountryNo (2 - Integer), City (8 - String), and Population (3 - Long). Refer to the previous step if necessary. On the General page, specify TData1 as the Master of TData2. Next, go to the Fields page and add a range condition to the CountryNo field by right-clicking it and selecting New Range Condition from the context menu. Type:

    Example Title
    Copy Code
    TData1.CountryNo
    

    in the Value Expression text box and

    Example Title
    Copy Code
    TData1.CountryNo <> 0
    

    in the Condition text box located to the right of it. This creates a range condition, TData2.CountryNo = TData1.CountryNo, connecting TData2 to its master, TData1. However, the range condition does not apply when TData1.CountryNo = 0, (that is, when Country = All).

  6. Set the DataMode property of TData3 to 1 - MemoryArray and define two fields: CountryNo and Country, with Data Type set to Integer and String, respectively. Follow the same instructions given in Step 4.

  7. Open the True DataControl property pages for TData3. Select TData2 in the Master combo box in the General page. Select 1 - Outer in the LinkType combo box. TData3 is now specified as linked to the master TData2 control.

  8. Create a range condition for the CountryNo field of TData3:

    Example Title
    Copy Code
    TData3.CountryNo = TData2.CountryNo
    

    Refer to Step 5 if necessary. This range condition will ensure that exactly one TData3 record corresponds to any given TData2 record. Note that TData3 and TData1 represent the same array: Countries. The code to be added to the form will reflect this. Press OK to apply the changes.

  9. Open the True DataControl property pages for TData2. Go to the Fields page and create a new field called CountryName. Click on the Linked radio button and select TData3.Country from the Linked Field combo box. Drag the CountryName field from the last position in the field list and drop it to the position before City. Press OK to save changes. A linked field, CountryName, has been created based on the Country field of TData3.

    The ComponentOne XArrayDB Object must be added to this project in order to provide pre-initialized data for the memory-based TData controls.

  10. From the Project menu,Select References... to display a list of available type library references. Check the box labeled ComponentOneXArrayDB Object (xadb8.ocx), then press the OK button.

  11. Declare two variables of type XArrayDB in the Declarations section of the form:

    Example Title
    Copy Code
    Dim Countries As New XArrayDB
    
    Dim Cities As New XArrayDB
    

    XArrayDB is a special ActiveX object used to implement memory-based storage in TData controls. Other ComponentOne products, such as True DBGrid and True DBList also use it.

  12. Add the following code to the Form_Initialize event:

    Example Title
    Copy Code
    Private Sub Form_Initialize()
    
       CountriesArray = Array("All", "Canada", "USA")
    
       Countries.ReDim LBound(CountriesArray), UBound(CountriesArray), 1, 2
    
       For I = LBound(CountriesArray) To UBound(CountriesArray)
    
          Countries(I, 1) = I
    
          Countries(I, 2) = CountriesArray(I)
    
       Next I
    
       CityNames = Array("New York", "Chicago", "Toronto", "Montreal")
    
       CountryNumbers = Array(2, 2, 1, 1)
    
       Population = Array(10000000, 5000000, 4000000, 3000000)
    
       Cities.ReDim LBound(CityNames), UBound(CityNames), 1, 3
    
       For I = LBound(CityNames) To UBound(CityNames)
    
          Cities(I, 1) = CountryNumbers(I)
    
          Cities(I, 2) = CityNames(I)
    
          Cities(I, 3) = Population(I)
    
       Next I
    
    End Sub
    

    This code fills the two XArrayDB objects, Countries and Cities, with data.

  13. Connect all three TData controls to their corresponding XArrayDB data sources by setting the Array property in their OpenData events:

    Example Title
    Copy Code
    Private Sub TData1_OpenData()
    
        Set TData1.Array = Countries
    
    End Sub
    
     
    
    Private Sub TData2_OpenData()
    
        Set TData2.Array = Cities
    
    End Sub
    
     
    
    Private Sub TData3_OpenData()
    
        Set TData3.Array = Countries
    
    End Sub
    

    Note that TData1 and TData3 are actually connected to the same array data source.

    The OpenData event is fired each time a TData control is about to be populated with data. This is where a pre-filled XArrayDB object should be assigned, so the TData control will already have some data before any controls that are bound to it are displayed.

     

Run the Program and Observe the Following:

Close the program. You have successfully completed Tutorial 13.

 

 


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

Product Support Forum  |  Documentation Feedback