delphicomboboxfiremonkeylivebindings

How to get Item.LookupData and SelectedValue (as integer) of an FMX TComboBox at runtime?


There is a ComboBox on the FMX Form. It is binded with a datasource (table that has an id-integer and speciality - varchar fields) in the following manner-

object LinkFillControlToField1: TLinkFillControlToField
      Category = 'Quick Bindings'
      Control = ComboBox1
      Track = True
      FillDataSource = BindSourceDB1
      FillValueFieldName = 'id'
      FillDisplayFieldName = 'speciality'
      AutoFill = True
      BufferCount = -1
      AutoBufferCount = False
      FillExpressions = <>
      FillHeaderExpressions = <>
      FillBreakGroups = <>
    end

It is simple to get access to the value of chosen speciality (from ComboBox1.Selected.Text) but I can not find a way to access the id value of the selected item without extra SQL requests. Where is it stored in TComboBox or its ListBox? Where is SelectedValue stored and how to get it (as integer)?


Solution

  • I am currently using the following way to resolve the issue. I handle OnFillingListItem event in the following way and store id number in ComboBox Items. I use Tag property though it is not actually good.

    procedure TForm1.LinkFillControlToField1FillingListItem(Sender: TObject;
      const AEditor: IBindListEditorItem);
    begin
      (AEditor.CurrentObject as TListBoxItem).Tag :=
        YourLookuptable.FieldByName('id').AsInteger;
    end;
    

    Later on I fetch the Item id from ListBox1.Selected.Tag. This gives me a reliable ID.