Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there some better way to copy all DataSet Fields and their properties to another DataSet?

Tags:

delphi

I'm cloning a TClientDataSet and I want to copy all the fields to the clone (which is a new DataSet), I know I can loop through the Fields and copy the info, or make 2 instances of my class and just clone the cursor, but is there some better way? Something like create a new DataSet and assign the fields info?

EDIT:

The following class helper method works for me:

procedure TDataSetHelper.CopyFieldDefs(Source: TDataSet);
var
  Field, NewField: TField;
  FieldDef: TFieldDef;
begin
  for Field in Source.Fields do
  begin
    FieldDef := FieldDefs.AddFieldDef;
    FieldDef.DataType := Field.DataType;
    FieldDef.Size := Field.Size;
    FieldDef.Name := Field.FieldName;

    NewField := FieldDef.CreateField(Self);
    NewField.Visible := Field.Visible;
    NewField.DisplayLabel := Field.DisplayLabel;
    NewField.DisplayWidth := Field.DisplayWidth;
    NewField.EditMask := Field.EditMask;

   if IsPublishedProp(Field, 'currency')  then
     SetPropValue(NewField, 'currency', GetPropValue(Field, 'currency'));

  end;
end;

Anyone has a better way for doing this?

like image 608
Fabio Gomes Avatar asked Dec 18 '08 18:12

Fabio Gomes


People also ask

What is the difference between DataSet clone () and DataSet copy ()?

DataSet. Copy copies both the structure (tables, relations etc) and the data. DataSet. Clone copies only the structure of the DataSet.

What is copy data set?

Copies a source ProDataSet object to a target ProDataSet object. By default, the AVM empties the target ProDataSet object temp-tables of all records before copying the source ProDataSet object.


1 Answers

If you just want to copy the field definitions you can do the following:

ds2.FieldDefs.Assign(ds1.FieldDefs);
ds2.CreateDataSet;
ds2.Open;

Of course this assumes you created FieldDefs for ds1.

like image 98
Jim McKeeth Avatar answered Nov 15 '22 07:11

Jim McKeeth