Skip to main content

Using Lookup Columns

  • 3 minutes to read

The lookup column allows records to be displayed and edited from a lookup dataset. This column type is represented by the LookupComboBox editor.

There are 2 ways to set up a lookup column.

The first method is to connect your DBColumn to a lookup field that was created in Delphi’s persistent field editor specifically for this purpose. For more information on how to create a persistent Lookup field see the Defining a lookup field topic in the Delphi documentation or the Using Lookup Editors topic in this help system. If a dataset contains a lookup field (which is properly set up) a user just has to assign the lookup field’s name to the DBColumn.DataBinding.FieldName property.

Another way of setting up a lookup column is to use the Properties or RepositoryItem properties of a column.

The PropertiesClass property allows you to choose a specific editor class for the column.

The RepositoryItem property allows you to choose from one you have previously prepared.

After assigning the required value to either of these properties, the Properties property will contain an object for configuring that kind of editor:

tvFilmsPersonsStaffPERSONID.PropertiesClass := TcxLookupComboBoxProperties;

If you choose the editor class LookupCombo you will get an editor class that will include properties for configuring a ListSource, ListColumns and KeyFieldNames and DataBinding.FieldName which replaces the LookupComboBox.DataBinding property (see section two in the topic Using Lookup Editors) and is used to match the value of the lookup dataset field (KeyFieldNames ).

Let us examine how lookup columns are configured in the MasterDetailTableDemo.

This demo introduces two grid controls which display data from a master and detail table (Grid and GridDetail, respectively). The GridDetail control shows data from the tblFilmsPersonsStaff table, which contains information about people involved in film production. There are two fields in this table: PERSONLINEID and PERSONID, which refer to key field values from other tables (tblPersonsLine and tblPersons, respectively). The Person column in the tvFilmsPersonsStaff View is set up to demonstrate the name of the person whose ID field in the tblPersons table matches the PERSONID field value in tblFimsPersonsStaff. The following image shows the design time settings applied to the editor of the Person lookup column:

tvFilmsPersonsStaffPERSONID.PropertiesClass := TcxLookupComboBoxProperties;
  with TcxLookupComboBoxProperties(tvFilmsPersonsStaffPERSONID.Properties) do
  begin
    ListSource := MasterDetailTableDemoMainDM.dsPersons;
    ListFieldNames := 'NAME';
    KeyFieldNames := 'ID';
  end;
  tvFilmsPersonsStaffPERSONID.DataBinding.FieldName := 'PERSONID';
//...

The following image shows the Person column at runtime:

See Also