Check if row was changed in a TClientDataset

倾然丶 夕夏残阳落幕 提交于 2019-12-10 14:22:12

问题


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?


回答1:


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.




回答2:


You don't have to loop through the dataset, you can use the ChangeCount property.



来源:https://stackoverflow.com/questions/38269301/check-if-row-was-changed-in-a-tclientdataset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!