Using DataWindow information to populate a TreeView
A useful
implementation of the TreeView control is to populate it with information
that you retrieve from a DataWindow. To do this your application will:
- Declare and instantiate
a DataStore and assign a DataWindow object - Retrieve information as needed
- Use the retrieved information to populate the TreeView
- Destroy the DataStore instance when you are finished
Because a TreeView can display different types of information
at different levels, you will probably define additional DataWindows,
one for each level. Those DataWindows usually refer to different
but related tables. When an item is expanded, the item becomes a
retrieval argument for getting child items.
Populating the first level
This example populates a TreeView with a list of composers.
The second level of the TreeView displays music by each composer. In the database
there are two tables: composer names and music titles (with composer
name as a foreign key).
This example declares two DataStore instance variables for
the window containing the TreeView control:
1 |
datastore ids_data, ids_info |
This example uses the TreeView control’s Constructor
event to:
- Instantiate the DataStore
- Associate it with a DataWindow and retrieve information
- Use the retrieved data to populate the root level
of the TreeView1//Constructor event for tv_1
1treeviewitem tvi1, tvi2
1long ll_lev1, ll_lev2, ll_rowcount, ll_row
1
1//Create instance variable datastore
1ids_data = CREATE datastore
1ids_data.DataObject = "d_composers"
1ids_data.SetTransObject(SQLCA)
1ll_rowcount = ids_data.Retrieve()
1
1//Create the first level of the TreeView
1tvi1.PictureIndex = 1
1tvi1.Children = TRUE
1//Populate the TreeView with
1//data retrieved from the datastore
1FOR ll_row = 1 to ll_rowcount
1tvi1.Label = ids_data.GetItemString(ll_row, &
1'name')
1This.InsertItemLast(0, tvi1)
1NEXT
Populating the second level
When the user expands a root level item, the ItemPopulate
event occurs. This script for the event:
- Instantiates a second
DataStore. Its DataWindow uses the composer name as a retrieval
argument for the music titles table. - Inserts music titles as child items for the selected
composer. The handle argument of ItemPopulate will be the parent
of the new items.1//ItemPopulate event for tv_1
1TreeViewItem tvi1, tvi2
1long ll_row, ll_rowcount
1
1//Create instance variable datastore
1ids_info = CREATE datastore
1ids_info.DataObject = "d_music"
1ids_info.SetTransObject(SQLCA)
1
1//Use the label of the item being populated
1// as the retrieval argument
1This.GetItem(handle, tvi1)
1ll_rowcount = ids_info.Retrieve(tvi1.Label)
1
1//Use information retrieved from the database
1//to populate the expanded item
1FOR ll_row = 1 to ll_rowcount
1This.InsertItemLast(handle, &
1ids_info.GetItemString(ll_row, &
1music_title'), 2)
1LOOP
Destroying DataStore instances
When the window containing the TreeView control closes, this
example destroys the DataStore instances:
1 |
//Close event for w_treeview |
1 |
DESTROY ids_data |
1 |
DESTROY ids_info |