Using a Grid Control in Its Default State

From RAD Studio
Jump to: navigation, search

Go Up to Viewing and Editing Data with TDBGrid

The 'State'property of the Columns property of the grid indicates whether persistent column objects exist for the grid. Columns.State is a runtime-only property that is automatically set for a grid. The default state is csDefault, meaning that persistent column objects do not exist for the grid. In that case, the display of data in the grid is determined primarily by the properties of the fields in the dataset of the grid, or, if there are no persistent field components, by a default set of display characteristics.

When the Columns.State property of the grid is csDefault, grid columns are dynamically generated from the visible fields of the dataset and the order of columns in the grid matches the order of fields in the dataset. Every column in the grid is associated with a field component. Property changes to field components immediately show up in the grid.

Using a grid control with dynamically-generated columns is useful for viewing and editing the contents of arbitrary tables selected at run time. Because the structure of the grid is not set, it can change dynamically to accommodate different datasets. A single grid with dynamically-generated columns can display a Paradox table at one moment, then switch to display the results of an SQL query when the DataSource property of the grid changes or when the DataSet property of the data source itself is changed.

You can change the appearance of a dynamic column at design time or runtime, but what you are actually modifying are the corresponding properties of the field component displayed in the column. Properties of dynamic columns exist only so long as a column is associated with a particular field in a single dataset. For example, changing the 'Width property of a column changes the 'DisplayWidth property of the field associated with that column. Changes made to column properties that are not based on field properties, such as Font, exist only for the lifetime of the column.

If a dataset of the grid consists of dynamic field components, the fields are destroyed each time the dataset is closed. When the field components are destroyed, all dynamic columns associated with them are destroyed as well. If a dataset of the grid consists of persistent field components, the field components exist even when the dataset is closed, so the columns associated with those fields also retain their properties when the dataset is closed.

Note: Changing the Columns.State property of a grid to csDefault at run time deletes all column objects in the grid (even persistent columns), and rebuilds dynamic columns based on the visible fields of the dataset of the grid.

See Also