0

In an existing app, a TClientDataSet "Band.CDS" has 3 fields defined using FieldDefs: "Name", "Genre" and "URL". Now want to add a 4th field.

At launch, FormCreate does this:

  appPath := ExtractFilePath(Application.ExeName);
  BandCDS.FileName := appPath + 'bands.xml'; // an existing file
  BandCDS.Active := TRUE;
  BandCDS.LoadFromFile(BandCDS.FileName);  // load from a file

A TDataSource points to BandCDS, and a TDBGrid points to that TDataSource.

Everything works fine. But I now want to add a 4th field to the TClientDataSet and have it shown to the user (along with the other fields) in the TDBGrid.

What does NOT work: simply adding a fourth item to the FieldDefs for BandCDS.

ADDED INFORMATION ---

At launch, The CDS is loaded from an XML file:

appPath := ExtractFilePath(Application.ExeName);
  BandCDS.FileName := appPath + 'bands.xml';

At the end of the session, the CDS is saved back out to the same file:

BandCDS.SaveToFile(BandCDS.FileName, dfXML);

After the file is loaded, I tried changing a field name and adding a new field:

BandCDS.FieldDefs[1].Name := 'NewName';
BandCDS.FieldDefs.Add('A',ftFixedChar,1);
  • After that when I examine the FieldDefs, the name HAS been changed, and the new field HAS been added
  • However, the DBGrid display is unchanged.
  • If I save the file (as mentioned above), exit, and re-launch, we're back to the original names and fields.
  • Clearly, the field revisions didn't affect what was output by the SaveToFile.

Thank you! Kevin

Kevin Killion
  • 123
  • 2
  • 9
  • is the file a CDS or an XML? – MundoPeter Jun 29 '23 at 20:54
  • 2
    If the new field is a read-only field you can add persistent fields to CDS plus one [calculated field](https://docwiki.embarcadero.com/RADStudio/en/Programming_a_Calculated_Field). Other than that you can either [modify XML before loading](https://stackoverflow.com/a/39883700/11562188) it into CDS or copy content of CDS into new dataset with extra field. – Peter Wolf Jun 30 '23 at 07:12
  • 1
    I tried both Peter's suggestions and can recommend copying the CDS, trying to fix the XML is too complex. – Jan Doggen Jun 30 '23 at 12:56
  • I’ve added further details to original post about CDS – Kevin Killion Jun 30 '23 at 20:51
  • Peter and Jan, THANK YOU. But I'm not sure what you mean by "copy content of CDS into new dataset with extra field". Does that mean create a new CDS, open both the old and new, and then write code to go through the entries and copy all the fields of each entry to the new CDS? Or is there something more elegant? THANK YOU. – Kevin Killion Jul 10 '23 at 16:04

0 Answers0