delphidatagriddrop-down-menutadodataset

Delphi - ADODataSet Subset


I am building a form in Delphi that has a dropdown of Services and a grid of Add-Ons for the select service. The data I am getting comes from an API and I am storing the data for the Services in an ADODataSet as follows:

ID (integer)
Name (string)
Description (string)
BasePrice (currency)
AddOns (array of AddOn, not currently stored in the ADODataSet)

I would like to have the grid populate with the AddOn data based on the selection from the dropdown (each Service has a different list of Add-Ons). How do I store the AddOn information so that it can be related back to the Service info? Do I need to create a second ADODataSet or is there a way to store it in the same ADODataSet as the Services?

The AddOns have the following fields:

ID (integer)
Name (string)
Description (string)
UnitPrice (currency)
Quantity (integer)

I am using Delphi 2005 and have Indy for Delphi.

EDIT

In digging around the Fields editor for Datasets I found that I can create a field of type 'DataSet'. Would this allow me to tie the two together? If so can someone explain how that is done?

I tried to do it by creating a second dataset (ADODataSetAddOns) and assigning the new dataset to the dataset field in the first dataset (ADODataSetServices.AddOns) but get the error message 'No matching ADO data type for Dataset', which I assume is referring to the AddOns field not finding a dataset.


Solution

  • You can use 2 datasets (master-detail) to show the relation between the Services and Addons and then using TClientDataset as memory dataset you can store the selections using the Service Id, Addon Id pair as Index, the structure of this client dataset can be created in runtime like this

      ClientDataSet1.FieldDefs.Clear;
      ClientDataSet1.FieldDefs.Add('IdService', ftInteger);
      ClientDataSet1.FieldDefs.Add('IdAddon ', ftInteger);
      //add morr fieldd here is you want 
      ClientDataSet1.IndexDefs.Add('Index1','IdService;IdAddon',[ixPrimary, ixUnique]);
      ClientDataSet1.IndexName:='Index1';
      ClientDataSet1.CreateDataSet;
    

    and finally when you need pass the data selected by the user to the service you can iterate over the ClientDataset just as any TDataset.