Check if string in TClientDataset has changed

I have a TClientDataset with n fields and I need to scroll through them to count how many of them have changed, but do:

if (Cds.fields1.Value <> Cds.fields1.OldValue) and (Cds.fields2.Value <> Cds.fields2.OldValue) etc.... 

or a loop through Cds.fields [I] is not very "clean"

Is there a Cds.RowChanged way or something else?

+5
source share
2 answers

To do this, you can use the TClientDataSet UpdateStatus property:

 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 line is saved after its changes have been sent back to CDS.Post CDS. Obviously, it is up to you which of these numbers you need for your application.

As indicated in the online help, you can use UpdateStatus to set the value of the 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 the StatusFilter property for the StatusFilter temporary filter to select rows with the UpdateStatus specification:

 procedure TCDSForm.Button1Click(Sender: TObject); begin if CDS.StatusFilter = [] then CDS.StatusFilter := [usModified] else CDS.StatusFilter := []; Caption := IntToStr(CDS.RecordCount); end; 

Note that a CDS.RecordCount with a StatusFilter set to usModified does not necessarily return the same value as the CDS.ChangeCount property, since the ChangeCount value includes the number of rows inserted as well as the number that was changed .

+11
source

You do not need to iterate over the data set; you can use the ChangeCount property.

+2
source

All Articles