Tutorials > Tutorial 22 - Reusable Layouts |
In True DBGrid, a layout comprises the complete set of properties that define the appearance and behavior of a grid, its splits, and its columns. You can save a grid layout to a binary file, then reuse it in other projects. At design time, you can store multiple layouts in a single file, then load them as needed in code. You can even store a layout file on a remote computer and have True DBGrid download it asynchronously, an ideal technique for initializing a grid on an HTML page.
In this tutorial, you will learn how to save and retrieve layouts at run time. If you have an Internet connection, you can also retrieve a layout file from the APEX server.
Start with the project created in Tutorial 1.
Add three command buttons (Command1, Command2, Command3) to the form (Form1)
Set the Caption property of Command1, Command2, and Command3 to "Retrieve Layout", "Save Layout", and "Retrieve ComponentOne Layout", respectively. Your form should now look like this.
Right-click TDBGrid1 to display its context menu.
Choose Properties to display the Property Pages dialog. Select the Splits tab. Expand the Splits(00) node and set the AllowColMove property to True.
Add the following code to the Click events of Command1, Command2 and Command3:
Example Title |
Copy Code
|
---|---|
Private Sub Command1_Click() On Error GoTo Err: ' Load the layout. TDBGrid1.LayoutName = "TestLayout" TDBGrid1.LayoutFileName = App.Path & "\Tutor22.grx" TDBGrid1.LoadLayout Exit Sub Err: MsgBox Err.Description End Sub Private Sub Command2_Click() ' Save current layout. TDBGrid1.LayoutFileName = App.Path & "\Tutor22.grx" TDBGrid1.Layouts.Add "TestLayout" End Sub Private Sub Command3_Click() ' Load layout from APEX. TDBGrid1.LayoutName = "APEX" ' Start the download. TDBGrid1.LayoutUrl = _ "ftp://ftp.apexsc.com/pub/files/tutor22.grx" ' Set the order of records (by Country). Data1.RecordSource = _ "Select * From Composer Order By Country" Data1.Refresh End Sub |
Place the following code in the LayoutReady event of TDBGrid1:
Example Title |
Copy Code
|
---|---|
Private Sub TDBGrid1_LayoutReady()
' Load layout as soon as it is available.
TDBGrid1.LoadLayout
End Sub
|
Run the program and observe the following:
As in Tutorial 1, True DBGrid retrieves the database schema information from the Data control and automatically configures itself to display the data for all fields in the database table.
Change the layout of the grid by modifying column widths and the row height. You can also rearrange the column order. When you are finished, click the Save Layout button. End the program and then restart it. The grid will display the default layout as before. Now, click the Retrieve Layout button. The grid will display the layout that you saved previously.
If your computer is connected to the Internet, click the Retrieve ComponentOne Layout button. The grid will be configured according to a layout file stored on the APEX server.
For more information, see Reusable Layouts.
This concludes Tutorial 22.