且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将表单的第二个实例绑定到数据模块的第二个实例?

更新时间:2023-02-04 21:56:09

看看这个问题:

使用delphi中的数据模块分离数据集实例



基本上,答案是创建DataModule,然后创建Form,然后将创建的DataModule的名称设置为空字符串。这将使初始数据绑定发生,但会阻止其他表单看到该模块。



此外,创建的下一个版本仍将具有原始名称(不需要数字后缀)。


I have a Form which has data aware controls bound to datasets in a datamodule. I need to create additional instances of both the form and datamodule in the same application.

I deleted the global variable that was automatically created when the data module was first added to the project. To my delight, controls in the designer could still be bound to datasets in the data module without this global variable. I'm assuming the IDE is parsing the dfm of the datamodule so the designer can still "see" the datamodule. (without the data module loaded in the IDE the list of data sources is empty)

When I create two instances of the form and two instances of the datamodule at runtime both instances of the form appear to be bound to only the first data module that was created. Inspecting the second instance of the data module reveals that the Name property has a number suffix that wasn't there at design time.

The form depends on a lot of datasets in the data module. Is there an easier way to bind the second form instance to the second data module's datasets without resorting to hand coded SomeControl.DataSource := Module2.dsSomeData for every single control?

I'm open to alternative suggestions as well. One solution would be to move the datasets to the form itself. Still it seems a shame if design time data binding only works on singletons.

Take a look at this question:
separate dataset instances using datamodules in delphi

Basically the answer is to create your DataModule, then your Form, then set the name of the created DataModule to an empty string. That will let the initial data binding happen but prevent other forms from seeing that Module.

In addition the next version that is created will still have the original name (no need for the numeric suffix).