custKey := TCustomerKey.Create; custKey.CustNo := dataset [ 'CustNo']; custKey.Company := dataset ['Company']; self.Add(custKey, TCustomer.Create (custKey)); dataset.Next; end; end;
The demo program has a main form and a data module hosting a Client-DataSet85. The main form has a ListView control that is filled when a user presses the only button.
After loading the data in the dictionary, the btnPopulateClick method uses an enumerator on the dictionary's keys:
Sender: TObject); var custkey: TCustomerKey; listItem: TListItem; begi n
DataModulel.ClientDataSetl.Active := True; CustDict.LoadFromDataSet(DataModulel.ClientDataSetl);
for custkey in CustDict.Keys do begi n listItem := ListViewl.Items.Add; listItem.Caption := custkey.Company; 1istItem.SubItems.Add(FloatTOStr (custkey.CustNo)); listItem.Data := custkey;
This fills the first two columns of the ListView control, with the data available in the key objects. Whenever a user selects an item of the ListView control, though, the program will fill a third column:
procedure TFormCustomerDictionary.ListViewlSe1ectItem( Sender: TObject; Item: TListItem; Selected: Boolean); var aCustomer: TCustomer; begi n aCustomer := CustDict.Items [Item.data]; Item.SubItems.Add(IfThen (aCustomer.State <> '', aCustomer.State + ' + aCustomer.Country, aCustomer.Country));
The method above gets the object mapped to the given key, and uses its data. Behind the scenes, the first time a specific object is used, the property access method triggers the loading of the entire data for the TCustomer object.
Was this article helpful?
What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.