Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String grid as buffer?

Status
Not open for further replies.

Euphaz

Programmer
Jul 12, 2001
40
0
0
NO
I want to import and edit a text file containing tab separated columns. I know how to get it into and display it in the string grid.

But is it possible to use the stringgrid for storing and editing the data or do i have to read the data into an 2D string array first (static buffer)?

Any suggestions?
 
Here's some code fragment for you. Assume that you've fetched a complete row from your file, tab separated, and stored it in
Code:
s
:
Code:
col := 0;
while Pos(#09, s) > 0 do begin
  if MyStringGrid.ColCount < col + 1 then MyStringGrid.ColCount := col + 1;
  MyStringGrid.Cells[col, row] := Copy(s, 1, Pos(#09, s)-1);
  s := Copy(s, Pos(#09, s)+1, 9999);
  Inc(col);
end;
MyStringGrid.Cells[col, row] := s;
So while there's a tab character still in
Code:
s
, break off the bit of
Code:
s
that's before the tab, and stick it in the next cell. Then throw away that piece of
Code:
s
. Iterate.

This fragment of course only works for one row; you need to wrap it in another loop for
Code:
row
from 0 to whatever. That loop probably begins
Code:
while not Eof(
-- Doug Burbidge mailto:dougburbidge@yahoo.com
 
I see no reason why you can't use the StringGrid to store and edit the data. You can access each entry seperately (Cells[i, j]) or use the TStringList for each row or column.
 
Thanks, it's working great! Just a pair of q's:

The grid are resized (col,row) according to the data in the file (ie File has 8 columns -> Grid.ColCount:=8). But if a file having different columns (from 2 to 8 cols) are loaded later, some data from the first file show up.

How can i clear the mem space beyond 'ColCount' that was used before, can i do things like this?:

&quot;file increased ColCount to 4&quot;
&quot;clearing 8 cells anyway, the next line might expand the columns&quot;

How much mem is each cell taking up?
 
You can set the ColCount and RowCount any time you want. You can set them to a smaller value, but a quick experiment shows that when you set them back to a higher value, the excess data has _not_ been cleared. So you'll have to loop through each row, clearing the excess:
Code:
for row := 0 to MyStringGrid.RowCount do
  while MyStringGrid.Rows[row].Count > MyStringGrid.ColCount do
    MyStringGrid.Rows[row].Delete(MyStringGrid.Rows[row].Count - 1);
So we loop through each row, and for that row, if there are too many elements in the row, repeatedly throw the last element away until it's just right.

As to size, you can see the size of any object with code like:
Code:
    ShowMessage(IntToStr(StringGrid1.InstanceSize));
But I'd guess that the underlying storgae of the data in a TSringGrid is a TStringList plus a TList for each row; so storage would be 1 byte per character, plus a small overhead. -- Doug Burbidge mailto:dougburbidge@yahoo.com
 
Thanks again. Is there a 'command' that clears the entire string grid like the program does initially?

The program should have such a line after user klicked 'open file', as the problem develop on the second file...
 
Code:
for row := 0 to MyStringGrid.RowCount do
    MyStringGrid.Rows[row].Clear;
MyStringGrid.RowCount := 0;
MyStringGrid.ColCount := 0;
In general, TStringList is an extremely nifty tool for the temporary storage of data in a myriad of different situations. Look up the help on it and take a browse through its properties and methods. Note that although the StringGrid uses TStrings, when you create your own you'll use TStringList's, because TStringList is the descendant that actually implements all the stuff you want. -- Doug Burbidge mailto:dougburbidge@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top