在设计时将 TDataSet 嵌入表单中

发布于 2024-09-28 00:58:18 字数 377 浏览 2 评论 0原文

我正在寻找一种方法来向 delphi 中的 TDBLookupComboBox 提供 ListSource,而无需在数据库服务器上有实际的表来从中绘制该列表。组合框的数据字段是一个 1 个字符字段,其中包含一个编码值,例如“A”=“驾驶执照”、“B”=“护照”、“C”=“图书馆卡”等。该表仅包含 A、B 或 C。应用程序负责在 GUI 中显示“驱动程序许可证”。通常一个数据库可能有一个查找表,但这个数据库没有,我无法添加一个。我的想法是,数据库查找控件的 DataSource 和 ListSource 不必是同一个数据库,因此,如果可以在我的表单中定义一个包含查找数据的小表,那么我可以使用该表不需要真正的数据库表。

有谁知道 delphi 组件允许在表单上定义 TDataSet 而无需在其后面包含任何实际数据文件?

I am looking for a way to provide a ListSource to a TDBLookupComboBox in delphi without having an actual table on the database server to draw that list from. The DataField for the Combo Box is a 1 character field that contains a coded value such as 'A' = 'Drivers License', 'B' = 'Passport', 'C' = 'Library Card', etc. That is to say that the table only contains A, B, or C. The application is responsible for Displaying 'Drivers License' in the GUI. Normally a database might have a look up table but this database does not and I can not add one. My idea is that the DataSource and ListSource for a DB Look-up control do not have to be the same database, so if it were possible to define a small table in my form that contains the look-up data then I could use that an not require a real database table.

Does anyone know of a delphi component that allows a TDataSet to be defined on a form without having any actual data files behind it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

晨敛清荷 2024-10-05 00:58:18

我知道内存中有不同的数据集。 Delphi 附带了 TClientDataSet,您可以按照您想要的方式使用它。您必须将 midas.dll 与可执行文件一起部署才能工作,或者必须在 use 子句中包含 MidasLib 以便在可执行文件中静态链接此库(运行时不需要 midas.dll)。

要从 TClientDataSet 获取所需内容,您可以创建字段并:

  • 将记录存储在 xml 文件中(例如使用您制作的另一个辅助工具)。在运行时使用 TClientDataSet 的 LoadFromFile 方法加载数据。此外,您可以使用 $R 指令将此 xml 存储为资源,并在运行时操作此资源,以向您的 ClientDataSet 提供包含的数据,以防止使用您的 exe 部署(以及可能的修改)xml 文件。
  • 使用 CreateDataSet 方法并在运行时使用所需内容插入/填充记录

代码示例:

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

I know there's different in-memory dataset. Delphi comes with TClientDataSet, which you can use the way you want. You have to deploy the midas.dll with your executable in order to work, or you must include the the MidasLib in your uses clause in order to statically link this library in your executable (no midas.dll needed at runtime).

To get what you want from the TClientDataSet, you can create fields and:

  • store the records in a xml file (for example with another auxiliary tool you made). At runtime load the data with the LoadFromFile method of the TClientDataSet. Additionally you can store this xml as a resource with the $R directive and manipulate this resource at runtime to feed your ClientDataSet with the contained data, to prevent the deployment (and possible modification) of the xml file with your exe.
  • use the CreateDataSet method and insert/populate records with what you want at runtime

code sample:

procedure TFrom1.Init;
begin
  cdsIDType.CreateDataSet;
  cdsIDType.InsertRecord('A', 'Drivers License');
  cdsIDType.InsertRecord('B', 'Passport');
  //etcetera.
end;
岁月如刀 2024-10-05 00:58:18

另一种解决方案是使用 TComboBox 而不是 TDBLookupComboBox。使用 TDictionary 定义简单的内存查找。

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's value in code.
  MyDataset.Fields.FieldByName('IDCard').AsString := ComboLookup[MyComboBox.Text];
end;

您可以使用 TComboBox.Items.AddObject 而不是单独的查找表,但您必须创建一个包装类来将 char 存储为 TObject 或使用 Chr 将其转换为整数,然后将其转换为 TObject 但上面是我认为更简单。

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

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's value in code.
  MyDataset.Fields.FieldByName('IDCard').AsString := ComboLookup[MyComboBox.Text];
end;

You could use TComboBox.Items.AddObject instead of a separate lookup table but you'd have to create a wrapper class to store a char as a TObject or use Chr to convert it to an integer then cast it to TObject but a the above is simpler in my opinion.

╭ゆ眷念 2024-10-05 00:58:18

使用 TClientDataset 并定义字段,然后连接到数据源。
在表单的 oncreate 事件中执行以下操作:
执行clientdataset的createdataset方法,然后用A、B、C数据填充它。

Use a TClientDataset and define the fields, then connect to a datasource.
In the oncreate event of the form do this:
execute the createdataset method of the clientdataset and then populate it with the A,B,C data.

空气里的味道 2024-10-05 00:58:18

如果您使用jvcl,则无需涉及数据集即可完成您想要的任务。只需使用 TjvDBComboBox,使用 Items 属性设置您希望 UI 显示的值,并使用 Values 属性设置存储在数据库中的实际值。

If you use the jvcl, what you want can be accomplished without involving a dataset. Just use a TjvDBComboBox, use the Items property to set the values you want the UI to display, and use the Values property to set the actual values stored in the database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文