• Sign in to Neowin Faster!

    Create an account on Neowin to contribute and support the site.

  • 0
Sign in to follow this  

[Delphi] Problem with SaveToFile

Question

tetsu-kun    1
Posted (edited)

Hey guys.

I'm having a problem with my code, ok I explain me more. I have the next visual design:

RAD.thumb.png.68c7b12a0a8980e4627684910dd092e0.png 

 

In my SaveDialog button I have the next code (added on the "Exportar" button):

procedure Button2Click(Sender: TObject);
var
  i: integer;
  strT: string;
  slst: TStringList;
begin                             
  slst:= TStringList.Create;
Try     
  With DBGrid1.DataSource.DataSet Do  
  begin                              
      First;
    while not Eof do   
    begin
      strT:= '"'+Fields[0].AsString+'"';
      for i:= 1 to FieldCount-1 do
        strT:= strT+',"'+Fields[i].AsString+'"';
      slst.Add(strT);               
      Next;
    end; 
          First;    
  end;   
  if SaveDialog1.Execute(0)then 
      slst.SaveToFile(SaveDialog1.filename);
Finally         
  slst.Free;
End;   
         
end; 

And its save me only the DBGrid, but I wanna save the full file, I mean, the Columns and content of the DBGrid and the operation result (on the bottom).

Can anybody explain me how to do it or what I'm doing wrong, please?

Thanks.

Regards.

Share this post


Link to post
Share on other sites

5 answers to this question

Recommended Posts

  • 1
+virtorio    3,324

You need to iterate though the DbSet Fields property and write out the FieldName in each field.

E.g.

  Field: TField; // in var section

  // put this before adding row values
  strT := '';
  for Field in DBGrid1.DataSource.DataSet.Fields do
    strT := '"' + Field.FieldName + '",';
  slst.Add(strT);

And you can add the value of the text box to the stringlist. 

On 6/27/2020 at 2:36 AM, Code Name: Lockdown said:

Wow, I didn't realise that Deplhi was still a language people used!  I always remember it being a competitor to VB6 (rightly or wrongly)

It's more popular than you'd think. You can also use recent versions to create macOS, iOS and Android apps. 

  • Like 2

Share this post


Link to post
Share on other sites
  • 0
Code Name: Lockdown    9,539

Wow, I didn't realise that Deplhi was still a language people used!  I always remember it being a competitor to VB6 (rightly or wrongly)

Share this post


Link to post
Share on other sites
  • 0
tetsu-kun    1
1 minute ago, Human.Online said:

Wow, I didn't realise that Deplhi was still a language people used!  I always remember it being a competitor to VB6 (rightly or wrongly)

Jajaja neither do I, but the report system that I use comes with this language and I need to know how can I do solve it 😛 I tried in some ways but I can't get the full report like I need it. That's the reason why I'm using Delphi jajaja, it's not because I wanna, it's because the system comes with it jajaja.

Regards.

  • Like 1

Share this post


Link to post
Share on other sites
  • 0
tetsu-kun    1

¿Cualquiera? : /

Share this post


Link to post
Share on other sites
  • 0
tetsu-kun    1
Posted (edited)

I got it :) after a lot of tries jejejeje

Now I need to print the TLabel with the Memo on the bottom (Total de Período en MN: and the Memo next it)

 

RAD.thumb.png.68c7b12a0a8980e4627684910dd092e0.png 

 

I can did to print the Memo as this way:

 

  With Memo1.DataSource.DataSet Do
  begin                              
      First;
    while not Eof do   
    begin             
      strT:= '"'+Fields[0].AsString+'"';
      for i:= 1 to FieldCount-1 do
        strT:= strT+',"'+Fields[i].AsString+'"';
      slst.Add(strT);               
      Next;  
    end; 
          First;    
  end;  

But I don't know how to do it whith the Label next with the Memo.

Could you help me please?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.