Working with Dataset Fields
Go Up to Using Object Fields
Dataset fields provide access to data stored in a nested dataset. The NestedDataSet property references the nested dataset. The data in the nested dataset is then accessed through the field objects of the nested dataset.
Displaying dataset fields
TDBGrid controls enable the display of data stored in data set fields. In a Vcl.DBGrids.TDBGrid control, a dataset field is indicated in each cell of a dataset column with the string "(DataSet)", and at run time an ellipsis button also exists to the right. Clicking on the ellipsis brings up a new form with a grid displaying the dataset associated with the current record's dataset field. This form can also be brought up programmatically with the DB grid's ShowPopupEditor method. For example, if the seventh column in the grid represents a dataset field, the following code will display the dataset associated with that field for the current record.
DBGrid1->ShowPopupEditor(DBGrid1->Columns->Items, -1, -1);
Accessing data in a nested dataset
A dataset field is not normally bound directly to a data aware control. Rather, since a nested data set is just that, a data set, the means to get at its data is via a TDataSet descendant. The type of dataset you use is determined by the parent dataset (the one with the dataset field.) For example, a BDE-enabled dataset uses TNestedTable to represent the data in its dataset fields, while client datasets use other client datasets.
To access the data in a dataset field
- Create a persistent Data.DB.TDataSetField object by invoking the Fields editor for the parent dataset.
- Create a dataset to represent the values in that dataset field. It must be of a type compatible with the parent dataset.
- Set that DataSetField property of the dataset created in step 2 to the persistent dataset field you created in step 1.
If the nested dataset field for the current record has a value, the detail dataset component will contain records with the nested data; otherwise, the detail dataset will be empty.
Before inserting records into a nested dataset, you should be sure to post the corresponding record in the master table, if it has just been inserted. If the inserted record is not posted, it will be automatically posted before the nested dataset posts.