Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to load and save a TStringGrid from/ to a stream Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
05-Nov-02
Category
VCL-General
Language
Delphi 2.x
Views
75
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

I have a form with 4 StringGrids which I fill at run time with data. It takes some 
time to enter the data and so I thought it would save me some time if I could save 
and load the form with its data - something I've never done in Delphi before. I've 
sorted the menu and the dialogue boxes but what method do I have to write to save 
the form, its StringGrids and the data therein - simply?

Answer:

You will have noticed that the StringGrid is a control that does not allow you to 
enter strings into it at design-time. The reason is that the streaming system 
cannot handle array properties like Cells, so the standard component streaming is 
no use for your task. But you can write your own routines to save a StringGrids 
content, of course. Something like this for example:
1   
2   procedure SaveGridToStream(aStream: TStream; aGrid: TStringGrid);
3   var
4     i, k: Integer;
5     iBuf: Integer;
6     S: string;
7   
8     procedure WrInt(anInt: Integer);
9     begin
10      aStream.WriteBuffer(anInt, Sizeof(anInt));
11    end;
12  
13  begin
14    with aGrid do
15    begin
16      WrInt(ColCount);
17      WrInt(rowCount);
18      for i := 0 to rowCount - 1 do
19        for k := 0 to colCount - 1 do
20        begin
21          S := Cells[k, i];
22          WrInt(Length(S));
23          if Length(S) > 0 then
24            aStream.WriteBuffer(S[1], Length(S));
25        end;
26    end;
27  end;
28  
29  procedure LoadGridFromStream(aStream: TStream; aGrid: TStringGrid);
30  var
31    i, k: Integer;
32    iBuf: Integer;
33    S: string;
34  
35    function RdInt: Integer;
36    begin
37      aStream.ReadBuffer(Result, Sizeof(Result));
38    end;
39  
40  begin
41    with aGrid do
42    begin
43      ColCount := RdInt;
44      RowCount := RdInt;
45      for i := 0 to rowCount - 1 do
46        for k := 0 to colCount - 1 do
47        begin
48          iBuf := RdInt;
49          if iBuf > 0 then
50          begin
51            SetLength(S, iBuf);
52            aStream.ReadBuffer(S[1], iBuf);
53            Cells[k, i] := S;
54          end;
55        end;
56    end;
57  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC