Paste TDataSet into the form at design time

I am looking for a way to provide a ListSource for a TDBLookupComboBox in delphi without having an actual table on the database server to retrieve this list. The DataField for Combo Box is a 1-character field that contains an encoded value, such as "A" = "License for Drivers", "B" = "Passport", "C" = "Library", etc. That is, the table contains only A, B or C. The application is responsible for displaying the "Driver License" in the graphical interface. Typically, a database may have a lookup table, but it does not exist in this database, and I cannot add it. My idea is that the DataSource and ListSource for managing database searches should not be the same database, so if you could define a small table in my form that contains the search data,I could use this does not require a real database table.

Does anyone know of a delphi component that allows you to define a TDataSet in a form without any actual data files behind it?

+3
source share
4 answers

An alternative solution is to use TComboBox rather than TDBLookupComboBox. Use TDictionary to define a simple search in memory.

type
  TMyForm = class(TForm)
    MyComboBox: TComboBox;
    MyDataset: TSimpleDataSet;
    procedure MyComboBoxChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    ComboLookup: TDictionary<string, Char>;
  end;

implementation

{$R *.dfm}

procedure TMyForm.FormCreate(Sender: TObject);
var
  Key: string;
begin
  ComboLookup := TDictionary<string, Char>.Create;
  ComboLookup.Add('Drivers License', 'A');
  ComboLookup.Add('Passport', 'B');
  ComboLookup.Add('Library Card', 'C');
  for Key in ComboLookup.Keys do
  begin
    MyComboBox.Items.Add(Key);
  end;
end;

procedure TMyForm.MyComboBoxChange(Sender: TObject);
begin
  // This may be wrong didn't bother to look
  //up the correct way to change a field value in code.
  MyDataset.Fields.FieldByName('IDCard').AsString := ComboLookup[MyComboBox.Text];
end;

You can use TComboBox.Items.AddObject instead of a separate lookup table, but you need to create a wrapper class for storing char as a TObject or use Chr to convert it to an integer and then pass it to TObject but, in my opinion, it's easier.

+2
source

, . Delphi TClientDataSet, , . midas.dll , , MidasLib , ( midas.dll ).

, TClientDataSet, :

  • XML (, ). LoadFromFile TClientDataSet. , xml $R , ClientDataSet , ( ) xml exe.
  • CreateDataSet / ,

:

procedure TFrom1.Init;
begin
  cdsIDType.CreateDataSet;
  cdsIDType.InsertRecord('A', 'Drivers License');
  cdsIDType.InsertRecord('B', 'Passport');
  //etcetera.
end;
+6

TClientDataset , . oncreate : createdataset clientdataset, A, B, C.

+1
source

If you use jvcl , then you can execute without using a dataset. Just use TjvDBComboBox, use the Items property to set the values ​​that the user interface should display, and use the Values ​​property to set the actual values ​​stored in the database.

-1
source

Source: https://habr.com/ru/post/1770192/


All Articles