检查是否在TClientDataset中更改了行 [英] Check if row was changed in a TClientDataset
问题描述
我有一个带有n个字段的TClientDataset,我必须遍历它们以计算有多少已更改但正在执行:
I have a TClientDataset with n fields and I have to loop through them to count how many have changed but doing:
if (Cds.fields1.Value <> Cds.fields1.OldValue) and (Cds.fields2.Value <> Cds.fields2.OldValue) etc....
或通过Cds.fields [I]循环不是很干净"
or looping through Cds.fields[I] is not very 'clean'
有Cds.RowChanged方法吗?
Is there a Cds.RowChanged method or something?
推荐答案
您可以为此使用TClientDataSet的UpdateStatus
属性:
You can use the TClientDataSet's UpdateStatus
property for this:
if Cds.UpdateStatus = usModified then
// current row was changed
其他可能的值是usUnmodified
,usInserted
和usDeleted
.与TDataSet.Modified
属性不同,当前行的UpdateStatus
在其更改已由CDS.Post发布回CDS之后仍然存在.显然,由您决定应用程序需要这些数字值中的哪一个.
Other possible values are usUnmodified
, usInserted
and usDeleted
. Unlike the TDataSet.Modified
property, the UpdateStatus
for the current row persists after its change(s) have been posted back to the CDS by CDS.Post. Obviously, it's up to you which of these number values you need for your application.
如联机帮助中所述,您可以使用UpdateStatus
设置计算字段的值:
As noted in the Online Help, you can use the UpdateStatus
to set the value of a calculated field:
procedure TForm1.ClientDataSet1CalcFields(DataSet: TDataSet);
begin
case TClientDataSet(DataSet).UpdateStatus of
usUnmodified: FieldByName('Status').AsString := '';
usModified: FieldByName('Status').AsString := 'M';
usInserted: FieldByName('Status').AsString := 'I';
usDeleted: FieldByName('Status').AsString := 'D';
end;
end;
您还可以使用其StatusFilter
属性临时过滤TClientDataSet以选择具有特定UpdateStatus
的行:
You can also use its StatusFilter
property to temporarily filter a TClientDataSet to select rows with a specfic UpdateStatus
:
procedure TCDSForm.Button1Click(Sender: TObject);
begin
if CDS.StatusFilter = [] then
CDS.StatusFilter := [usModified]
else
CDS.StatusFilter := [];
Caption := IntToStr(CDS.RecordCount);
end;
请注意,将StatusFilter
设置为usModified
的CDS.RecordCount
不一定返回与CDS.ChangeCount
属性相同的值,因为ChangeCount
值包括已插入的行数>以及已修改的号码.
Note that CDS.RecordCount
with the StatusFilter
set to usModified
does not necessarily return the same value as the CDS.ChangeCount
property, because the ChangeCount
value includes the number of rows inserted as well as the number that have been modified.
这篇关于检查是否在TClientDataset中更改了行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!