简体   繁体   English

如何复制包含其计算字段的Delphi TTable?

[英]How can I copy a Delphi TTable including its calculated fields?

I have defined a Delphi TTable object with calculated fields, and it is used in a grid on a form. 我已经定义了一个带有计算字段的Delphi TTable对象,并将其用于表格上的网格中。 I would like to make a copy of the TTable object, including the calculated fields, open that copy, do some changes to the data with the copy, close the copy, and then refresh the original copy and thusly the grid view. 我想制作一个TTable对象的副本,包括计算所得的字段,打开该副本,对该副本进行数据更改,关闭该副本,然后刷新原始副本,从而刷新网格视图。 Is there an easy way to get a copy of a TTable object to be used in such a way? 是否有一种简单的方法来获取以这种方式使用的TTable对象的副本?

The ideal answer would be one that solves the problem as generically as possible, ie, a way of getting something like this: 理想的答案是尽可能通用地解决该问题的方法,即一种获得以下内容的方法:

newTable:=getACopyOf(existingTable);

You can use the TBatchMove component to copy a table and its structure. 您可以使用TBatchMove组件复制表及其结构。

Set the Mode property to specify the desired operation. 设置Mode属性以指定所需的操作。 The Source and Destination properties indicate the datasets whose records are added, deleted, or copied. Source和Destination属性指示添加,删除或复制其记录的数据集。 The online help has additional details. 联机帮助具有其他详细信息。

(Although I reckon you should investigate a TClientDataSet approach - it's certainly more scalable and faster). (尽管我认为您应该研究一种TClientDataSet方法-它肯定更具可伸缩性和更快性)。

Let me propose several things: 让我提出几件事:

Let us suppose that you want to make changes programmatically. 让我们假设您要以编程方式进行更改。 You could then use DisableControls and EnableControls methods of the TTable to disallow screen updates during that time. 然后,您可以使用TTable的DisableControls和EnableControls方法禁止在此期间的屏幕更新。

If you want to have two screens with the same data (fe to compare data during online changes), you could actually create the same screen twice, with the TTable object being on the screen itself. 如果要使两个屏幕具有相同的数据(例如,要在联机更改期间比较数据),则实际上可以创建两次相同的屏幕,其中TTable对象位于屏幕本身。 It will have the exact same configuration (but not carry over previously made changes on the first screen but read the data from the database). 它将具有完全相同的配置(但不会在第一个屏幕上保留先前进行的更改,而是从数据库中读取数据)。 Changes made on one screen will not be automatically refreshed on the other. 在一个屏幕上所做的更改不会在另一屏幕上自动刷新。

Another way: Try using TDataSetProvider with TTable as Dataset (source) feeding a TClientDataSet. 另一种方法:尝试将TDataSetProvider与TTable一起用作提供TClientDataSet的数据集(源)。 ApplyUpdates would feed back the changes to the TTable. ApplyUpdates会将更改反馈到TTable。 Since the calculated fields are read only, they are not affected. 由于计算的字段是只读的,因此它们不受影响。 (untested, but should work) (未经测试,但应该可以工作)

You should be able to select the table on the form, copy it using Ctrl-C, then paste it into any text editor. 您应该能够选择表单上的表格,使用Ctrl-C复制它,然后将其粘贴到任何文本编辑器中。 You will get the text version of the object's properties which you can then edit as needed. 您将获得对象属性的文本版本,然后可以根据需要进行编辑。 When you are done, select all the text again and you can copy it to the clipboard and paste it back onto a form. 完成后,再次选择所有文本,然后可以将其复制到剪贴板并将其粘贴回表单。

I believe that the second approach (TClientDataset) is probably the best method to use in this scenario. 我相信第二种方法(TClientDataset)可能是在这种情况下使用的最佳方法。 An alternative would be to use a memory table ( kbmMemTable for instance). 另一种选择是使用内存表(例如kbmMemTable )。 Either way, you would clone your original table and then after making your changes loop thru the memory version of your dataset and update your original table. 不管哪种方式,您都将克隆原始表,然后在进行更改之后遍历数据集的内存版本并更新原始表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM