0

I'm creating FieldDefs at runtime for a TClientDataSet. Still at runtime I want to remove all FieldDefs. I'm saving TClientDataSet physically to a disc file. I tried removing existing FieldDefs using the following code so I could add new ones. But it didn't work:

with fDataSet do begin
    Active := False;
    DisableControls;
    FieldDefs.Clear;
    Fields.Clear;
    EnableControls;
end;

After executing this code, FieldDefs and Fields count are 0, but if I close and reopen the disc file, FieldDefs and Fields are still there.

What is the right way to change FieldDefs and Fields?

Disillusioned
  • 14,635
  • 3
  • 43
  • 77
user2383818
  • 709
  • 1
  • 8
  • 19
  • FieldDefs and Fields have to exist in order to save the `TClientDataSet` into a file. What else would it be saving? It couldn't contain any indexes or data without fields to base them on. I don't understand what you're actually trying to do here. "I want to create a TClientDataSet at runtime, remove all the fields, and save it to disk"? Can you [edit] to clarify what you're actually trying to accomplish? – Ken White Jan 22 '14 at 20:47
  • If you want more attention on your question you should add the `delphi` tag – Sir Rufo Jan 22 '14 at 20:56
  • Hi Ken. Let's say I have a `cds` file previously created. At a given moment, I want to `programmatically` change the `fields deffinitions` with a new `deffinition's set`. I think that the only way I can achieve this, is by removing `all existing deffinitions`, and afterwards, adding the `new deffinition's set`. Is that right? – user2383818 Jan 22 '14 at 21:06
  • Hi Sir Rufo. Sorry. I added the tag `delphi-xe`, wich has the word `delphi`. I suppoused it is was enough to identify the subject. In the future, I'll add the tag `delphi` too. – user2383818 Jan 22 '14 at 21:09
  • @SirRufo: I missed that the tag wasn't there. I should have added it also. – Ken White Jan 22 '14 at 21:41
  • @user2383818 I've edited your question to reflect what you're ***actually*** trying to achieve based on your comment (http://stackoverflow.com/questions/21293186/delphi-removing-fieldefs-and-fields-definitions-form-a-tclientdataset/21295035#comment32089902_21293186). For future reference, it's sometimes very important when you're stuck to step back and rethink what your ***actual objectives*** are. Especially when seeking help, as others may be able to offer an entirely different approach. – Disillusioned Jan 22 '14 at 22:16
  • @KenWhite Some users look only at `[delphi]` tagged questions and some miss to tag their question with it. I don't know what is the best way: tag all relevant questions with `delphi` or tell all to search for `[delphi*]` tag ;o) – Sir Rufo Jan 22 '14 at 22:33
  • @SirRufo: The question should be tagged delphi, as it's asking nothing specific to a certain version. (The answer would be applicable to all versions that contain TClientDataSet.) I just missed the fact it didn't have it when I saw the question originally, or I would have added it (which I did later). – Ken White Jan 22 '14 at 22:42
  • I'm sorry. In fact was a conceptual error. To achieve my goal, I have just to free the `TClientDataSet` and recreate it. Then I have a clean `TClientDataSet`. At this point, I can add a new set of field definitions. When I'm happy with the current field definitions set, I can command to save it to the physical file on disk. This approach is working fine. I thank all the contributors for their suggestions. I have to work out if is worth saving all other collections and properties, such as indexes and filters, and reapply them to the recently created `TClientDataSet`. But this is another matter. – user2383818 Jan 22 '14 at 22:43
  • Please forget my previous comment. Theres no need to recreate the `TClientDataSet`. Just `SaveToFile` when changes are OK. – user2383818 Jan 23 '14 at 00:52

2 Answers2

2

You'll have to pump the data from the old data set structure into the new along the following lines:

  • Define a new client dataset with the new structure.
  • Open your old client data set.
  • Iterate the old data set, and insert a record in the new one, performing any calculations and default assignments as you go.

E.g.

while not FOldDataSet.Eof do
begin
  FNewDataSet.Insert;
  FNewDataSet['FIELD1'] := FOldDataSet['FIELD3'];
  FNewDataSet['FIELD2'] := CDefaultFIELD2;
  FNewDataSet.Post;

  FOldDataSet.Next;
end;
Disillusioned
  • 14,635
  • 3
  • 43
  • 77
1

Consecutive Open recreates fields from internal Dataset. Just clear old field defs, add new ones and recreated dataset:

...
CDS.FieldDefs.Clear;
CDS.Fields.Add(...);
...
CDS.Fields.Add(...);
CDS.CreateDataSet;
...
pf1957
  • 997
  • 1
  • 5
  • 20