Working with Levels
- 4 minutes to read
Levels in the ExpressQuantumGrid are used to set up the data structure. Each level is associated with a View representing data in a specific form (for instance, a Table View or Card View).
The Structure Navigator and Component Editor allow you to
create and delete levels;
create and delete Views and associate them with levels;
create columns within tabular Views (or rows in Card Views) manually or by retrieving the fields from a linked data source.
When selecting a level or a View in the Structure Navigator or the Component Editor, its properties are automatically displayed in the Object Inspector. You can also click the level’s tab in a grid to quickly select this level.
Root Levels
A grid control enables you to display data from several independent data sources. It provides the Levels property, which maintains a collection of grid levels at the root nesting level.
By default a grid control contains a single root grid level when created. At design time, you can use the Structure Navigator to add root grid levels. Right-click the grid label and select the Add Level option
or via code:
After adding a root level at design time, the Structure Navigator looks like:
The first root grid level is addressed by Levels[0], the second – by Levels[1] and so on. Each level may contain child levels. This allows you to create master-detail relationships.
Child Levels
Child (or detail) levels are used to present master-detail relationships between master and detail tables. To add a child to a specific level at design time, right-click its label in the Structure Navigator and select the Add Level option.
or write code as follows:
At design time you will see the result:
To add another child level, just repeat the operation.
The Master-Detail topic describes the changes that need to be made to the View’s properties to set up a master-detail relationship between tables.
Associate a Level with a View
To display data, a level must be associated with a View via the TcxGridLevel.GridView property. You can assign one of the existing Views or create a new View.
To create a new View and assign it to a level at design time, you can right-click its label and select the Create View option. It will invoke the list of Views:
You can create a View without assigning it to a level via the Component Editor. Activate the Component Editor and switch to the Views panel. Then press the Add View button and select the type of View you want to add.
To assign an existing View to a level, right-click the level to invoke its context menu and select the required View from a list of the existing Views:
Alternatively, you can select a level, open the Object Inspector to see the level’s properties and set its GridView property:
Use the following code to create a View at runtime and assign it to a level. You should call the CreateView method, passing a View class as a parameter, and this will add a new View to the grid’s Views collection.
var
AView: TcxGridDBTableView;
//...
AView := TcxGridDBTableView(cxGrid1.CreateView(TcxGridDBTableView));
cxGrid1Level2.GridView := AView;
After assigning a View to a level at design time, the Structure Navigator looks like:
Switching Between Levels At The Same Hierarchy Level
By default, a grid control does not allow a user to switch between levels placed at the same nesting level. However, you can enable internal tabbed pages for this purpose. To allow tabs to be displayed at the root nesting level, use the grid’s RootLevelOptions.DetailTabsPosition property. If you want to display tabs to switch between the children of a specific level, use its Options.DetailTabsPosition attribute.
The following code displays root tabs at the top of the grid control:
Additionally, you should supply captions for the levels that are defined by the TcxGridLevel.Caption attribute.
The following code assigns captions to two root levels at runtime:
Now the grid control displays: