How to Modify Field Values in TDataSetProvider.OnUpdateData

早过忘川 提交于 2019-12-01 12:22:29

Aren't

if DataSet.UpdateStatus = usUnmodified then begin

and

if DataSet.UpdateStatus in [usInserted, usModified] then

Mutually exclusive? You may have a misplaced end - or lack of an else.

Well, let's start from beginning.

In delta, modifications are recorded (in my experience with it):

  • Deleted and Inserted records storage differ only on UpdateStatus
    • 1 record with the respective status
  • Edited record are stored differently - 2 records are stored that way (and IN THAT ORDER)
    • 1 record with UpdateStatus = usUnModified
    • 1 record with UpdateStatus = usModified - this record have only the values of the modified fields. The other field are all empty.

How to proceed an Delta modification

For Inserted/Deleted records

Set StatusFilter to [usInserted] and/or [usDeleted]. Alter they. You're done.

For Modified Records

Set SetStatusFiler to [usUnModified,usModified] to see both records in Delta. Do a While not DSDelta.Eof do and for each UpdateStatus = usUnModified do your test. If yes, you proceed with modification in the next record (the UpdateStatus = usModified corresponding to the one you tested). Else, you look for the next record with UpdateStatus = usUnModified.

EDIT: You're right. There's no way to change an field on delta of a modified record if you doesn't include all the fields that are marked required.

You're code goes to:

procedure TDBNextDocNo.DSPUpdateData(Sender: TObject; DataSet: TCustomClientDataSet);
begin
  DataSet.First;
  while not DataSet.EOF do begin
    if DataSet.UpdateStatus = usUnmodified then begin
      TPacketDataSet(Dataset).InitAltRecBuffers(True);
      if DataSet.UpdateStatus in [usInserted, usModified] then begin
        Dataset.Edit;
        DataSet.FindField('MyField').AsString := 'zzz';

        If Dataset.UpdateStatus in [usModified] then
        begin
          for i = 0 to Dataset.FieldCount - 1 do
          begin
            If Dataset.Fields[i].Name <> 'MyField' then
            begin
              If Dataset.Fields[i].Required then
                Dataset.Fields[i].Value := Dataset.Fields[i].OldValue; 
            end; 
          end;
        end;
        Dataset.Post;
      end;
    end;
  end;
  DataSet.Next;
end;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!