You can hack a DataSet to change its Modified property to AfterInsert / AfterEdit (and set the initial / default values), and then check on the DataSet.Modified (for example, before publishing).
To determine which specific fields have been changed, I keep a copy of the initial entry, for example:
type TDataRecord = array of record FieldName: string; Value: Variant; end; type TForm1 = class(TForm) ... private FInitRecord, FPostRecord: TDataRecord; end; function GetDataRecord(DataSet: TDataSet): TDataRecord; var I: Integer; begin Result := nil; if Assigned(DataSet) then begin SetLength(Result, DataSet.FieldCount); for I := 0 to DataSet.FieldCount - 1 do begin Result[I].FieldName := DataSet.Fields[I].FieldName; Result[I].Value := DataSet.Fields[I].Value; end; end; end; type TDataSetAccess = class(TDataSet); procedure TForm1.ADODataSet1AfterInsert(DataSet: TDataSet); begin // set initial values ADODataSet1.FieldByName('PK').Value := GetMyPKValue; ADODataSet1.FieldByName('DateCreated').AsDateTime := Now(); // un-modify TDataSetAccess(ADODataSet1).SetModified(False); // save initial record FInitRecord := GetDataRecord(ADODataSet1); end; procedure TForm1.ADODataSet1BeforePost(DataSet: TDataSet); var I: Integer; begin if ADODataSet1.Modified then begin FPostRecord := GetDataRecord(ADODataSet1); Memo1.Lines.Clear; for I := 0 to Length(FPostRecord) - 1 do begin if FPostRecord[I].Value <> FInitRecord[I].Value then Memo1.Lines.Add(Format('Field %s was modified', [FPostRecord[I].FieldName])); end; end; end;
Well, this is an abstract idea anyway. You can subclass your TDataSet , like me, and implement this function directly inside your TDataSet component.
kobik
source share