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....
or looping through Cds.fields[I] is not very 'clean'
Is there a Cds.RowChanged method or something?
You can use the TClientDataSet's UpdateStatus
property for this:
if Cds.UpdateStatus = usModified then
// current row was changed
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.
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;
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;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With