当数据集已处于插入状态时,如何确定数据感知组件字段是否已被修改?我想知道一个字段是否“真正”修改过. (我不在乎用户是否在某个字段中输入了某些东西,然后删除了所有内容,这意味着发生了修改).
DataSet.Modified,DataSet.UpdateStatus或ChangeCount没有解决我的问题.
LE:让我更深入地解释一下.所以,初始数据集看起来像
- -------------------------------------
- |PK | Field1| Field2| Field3|Field4|
- -------------------------------------
- | 1 | a | b | c | d |
- -------------------------------------
插入后
- -------------------------------------
- |PK | Field1| Field2| Field3|Field4|
- -------------------------------------
- | 2 | | | | |
- -------------------------------------
- | 1 | a | b | c | d |
- -------------------------------------
当数据集真正被修改时
- -------------------------------------
- |PK | Field1| Field2| Field3|Field4|
- -------------------------------------
- | 2 | avalue| | | |
- -------------------------------------
- | 1 | a | b | c | d |
- -------------------------------------
解决方法
您可以破解DataSet以在AfterInsert / AfterEdit上更改它的Modified属性(并设置初始值/默认值),然后测试DataSet.Modified(例如在发布之前打开).
为了确定哪些特定字段被修改,我持有初始记录的副本,例如:
为了确定哪些特定字段被修改,我持有初始记录的副本,例如:
- 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;
好吧,无论如何,这是抽象的想法.您可以像我一样对您的TDataSet进行子类化,并直接在您的TDataSet组件中实现此功能.