解决ClientDataSet连接脱离框架导致某些字段无法在客户端修改值的问题
function: 解决ClientDataSet连接脱离框架导致某些字段无法在客户端修改值的问题
description: 将一个复制Data的clientdataset做成可写。因为直接赋值cdsData.data=cdsTemp.data会连着datasetprovider的一些东西一起赋值,readonly属性也会赋值
error1: EDatabaseError with message "Field "AA" cannot be modified
如果仅仅修改cdsA.field[0].ReadOnly=false,在Post的时候回报error2
error2: EDBClient with message "Trying to modify read-only field
error3: Field "AA" must have a value
}
procedure SetCdsAllFieldCanEdit(cdsData: TClientDataSet);
var
cdsTmp: TClientDataSet;
I: Integer;
begin
cdsTmp := TClientDataSet.Create(nil);
try
cdsData.DisableControls;
cdsTmp.Data := cdsData.Data;
cdsData.Close;
cdsData.FieldDefs.Clear;
for I:=0 to cdsTmp.FieldDefs.Count - 1 do
begin
with cdsData.FieldDefs.AddFieldDef do
begin
DataType := cdsTmp.FieldDefs[I].DataType;
Size := cdsTmp.FieldDefs[I].Size;
Name := cdsTmp.FieldDefs[I].Name;
end;
end;
cdsData.CreateDataSet;
with cdsTmp do
begin
First;
while not Eof do
begin
cdsData.Append;
for I := 0 to Fields.Count - 1 do
cdsData.Fields[I].Value := Fields[I].Value;
Next;
end;
end;
if cdsData.State in [dsInsert, dsEdit] then
cdsData.Post;
cdsData.MergeChangeLog;
finally
cdsData.EnableControls;
cdsTmp.Free;
end;
end;