问题描述
我有一个自定义的TDatasetProvider,它允许为它提供的任何数据创建新字段.
I have an custom TDatasetProvider that allows to create new fields to whatever data it provides.
因此,假设您在原始数据集中找到了以下字段:
So, let's say you got the folowing fields on the original dataset:
- CustomerId
- 名称
- 年龄
您需要使用显示位图在DBGrid上选择它.好吧,因为我的DSP可以添加数据集数据的名为Selected
的布尔字段.
And you need to select it on DBGrid using showing an Bitmap. Well, you can since my DSP can addan boolean field called Selected
to the dataset data.
我现在的操作方式:
- 创建2个TClientDataset对象(来源和目标)
- 在Origin中,我加载了从InternalGetRecords方法的参数获取的数据(覆盖了它)
- 在Target中,创建从Origin数据集定义的fielddef,并添加开发人员在设计时创建的fielddef.
- 在目标上执行CreateDataset
- 然后逐行(逐字段)将数据从Origin数据库复制到Target数据集
- 最后,将Data变量作为InternalGetRecords的返回值返回.
我真的不知道是否有更优雅(更快)的方式来做到这一点.还有另一种(更快和/或更优雅)的方法来获得结果?
I really don't know if there's a more elegant (and faster) way to do that. There's another (faster and/or elegant) way to get that result?
推荐答案
似乎从源数据集中加载数据后,您可以调用 IDSBase.AddField
来添加更多字段:
It seems that after loading the data from the source dataset, you can call IDSBase.AddField
to add more fields:
uses
DB, DBCommon, DBClient, DSIntf;
type
THackClientDataSet = class(TClientDataSet);
procedure EncodeFieldDesc(var FieldDesc: DSFLDDesc;
const Name: string; DataType: TFieldType; Size, Precision: Integer;
Calculated: Boolean; Attributes: TFieldAttributes);
begin
// ... copied from TClientDataSet.EncodeFieldDesc
end;
//...
var
FldDesc: DSFLDDesc;
begin
FillChar(FldDesc, SizeOf(FldDesc), 0);
EncodeFieldDesc(FldDesc, 'SELECTED', ftBoolean, 0, 0, False, []);
with THackClientDataSet(DataSet) do
Check(DSBase.AddField(@FldDesc));
// now you can create a second client dataset and assign it DataSet.Data directly:
// DataSet2.Data := DataSet.Data;
// DataSet2 now contains the new field (with empty values in all existing records)
end;
我没有对其进行彻底的测试,但是上面的简单示例按预期工作,我能够像往常一样浏览第二个客户端数据集并编辑所有字段的值.
I didn't test it thoroughly but the simple example above worked as expected, I was able to navigate the second client dataset and edit the values of all fields as usual.
这篇关于将数据从数据集结构移动到另一个结构的更快方法(在TDatasetProvider中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!