//
ClientDataSet1.FieldDefs.Clear;
ClientDataSet1.FieldDefs.Add('doc_id', ftString,20); ClientDataSet1.FieldDefs.Add('doc_name', ftString, 20); ClientDataSet1.CreateDataSet; //创建虚拟表 ClientDataSet1.Open; ClientDataSet1.Append; ClientDataSet1.FieldByName('doc_name').AsString := 'aa'; ClientDataSet1.FieldByName('doc_id').AsString:= '11'; ClientDataSet1.Post; ClientDataSet1.AppendRecord(['22','bb']); DataSource1.DataSet := ClientDataSet1; cx_test.DataController.DataSource := DataSource1;function Tfrm_doc_sum.CreateTableInMemory(
const AFieldDefs: TFieldDefs): TDataSet;var TempTable:TClientDataSet;begin TempTable:=nil; Result:=nil; if AFieldDefs<>nil then begin try TempTable:=TClientDataSet.Create(Application); TempTable.FieldDefs.Assign(AFieldDefs); TempTable.CreateDataSet; Result:=(TempTable as TDataSet); Except if TempTable<>nil then TempTable.Free; Result:=nil; raise; end; end;end;procedure TForm1.btn1Click(Sender: TObject);
var
ADataSet:TDataSet;
begin
ADataSet:=TDataSet.Create(Self);
with ADataSet.FieldDefs do begin Add('item',ftString,40,False); Add('is_nh',ftString,10,False); Add('dosage',ftString,10,False); Add('item_num',ftString,10,False); Add('item_id',ftString,20,False); Add('price',ftString,20,False); Add('spec',ftString,20,False); Add('unit',ftString,10,False); Add('times',ftString,10,False); Add('days',ftString,10,False); end;{
with DataSource1 do
begin DataSet:=CreateTableInMemory(ADataSet.FieldDefs); DataSet.Open; DataSet.Append; DataSet.FieldByName('item').AsString:='Woody'; DataSet.FieldByName('item_id').AsInteger:=9527; DataSet.FieldByName('dosage').AsString:='Woody'; DataSet.FieldByName('item_num').AsInteger:=9527; DataSet.FieldByName('price').AsString:='Woody'; DataSet.FieldByName('spec').AsInteger:=9527; DataSet.FieldByName('unit').AsString:='Woody'; DataSet.FieldByName('times').AsInteger:=9527; DataSet.FieldByName('days').AsString:='Woody'; DataSet.FieldByName('is_nh').AsInteger:=9527; DataSet.Post; end;}
------清空内存表
1.clientdataset.EmptyDataSet;