Recent

Author Topic: StringGrid lock fixed column  (Read 3043 times)

christensen

  • Full Member
  • ***
  • Posts: 127
StringGrid lock fixed column
« on: September 12, 2016, 10:57:11 am »
Hi all,

I’m trying to populate a StringGrid from a TstringList, but I don’t want to fill the first column and row.
Those I want to keep for header and row index.

Code: Pascal  [Select][+][-]
  1. procedure tfrmMain.PopulateStringGrid;
  2. var
  3. TextFile, Line: TStringList;
  4.  Row, Col: Integer;
  5. begin
  6.  
  7.    grd_list.RowCount := 0;//clear any previous data
  8.  
  9.   TextFile := TStringList.Create;
  10.   try
  11.     Line := TStringList.Create;
  12.     try
  13.       Line.Delimiter := ' ';
  14.       TextFile.LoadFromFile(C_FNAME);
  15.  
  16.       grd_list.RowCount := TextFile.Count;
  17.       for Row := 0 to TextFile.Count-1 do
  18.       begin
  19.         Line.DelimitedText := TextFile[Row];
  20.  
  21.         for Col := 0 to grd_list.ColCount-1 do
  22.           if Col<Line.Count then
  23.             grd_list.Cells[Col, Row] := Line[Col]
  24.           else
  25.             grd_list.Cells[Col, Row] := 'N/A';
  26.  
  27.       end;
  28.  
  29.     finally
  30.       Line.Free;
  31.     end;
  32.   finally
  33.     TextFile.Free;
  34.   end;
  35.  
  36. end;                                              
  37.  
Lazarus 1.4.4, FPC 2.6.4, Windows 7 64bit, AMD Athlon 7750 black edition, Asus M3N78-CM
Lenovo L540 i7-4702MQ, Windows 10 x64,Lazarus 1.6.2,FPC 3.0

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: StringGrid lock fixed column
« Reply #1 on: September 12, 2016, 06:59:24 pm »
Just replace:
Code: Pascal  [Select][+][-]
  1. grd_list.Cells[Col, Row] := Line[Col]
with
Code: Pascal  [Select][+][-]
  1. grd_list.Cells[Col+1, Row+1] := Line[Col]

Or instead of magic "1" use
Code: Pascal  [Select][+][-]
  1. Col+grd_list.FixedCols, Row+grd_list.FixedRows
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

christensen

  • Full Member
  • ***
  • Posts: 127
Re: StringGrid lock fixed column
« Reply #2 on: September 14, 2016, 09:37:31 am »
Thanks but i've tried and doesnt work.

Lazarus 1.4.4, FPC 2.6.4, Windows 7 64bit, AMD Athlon 7750 black edition, Asus M3N78-CM
Lenovo L540 i7-4702MQ, Windows 10 x64,Lazarus 1.6.2,FPC 3.0

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: StringGrid lock fixed column
« Reply #3 on: September 14, 2016, 11:18:59 am »
Code: Pascal  [Select][+][-]
  1. procedure tfrmMain.PopulateStringGrid;
  2. var
  3.   TextFile, Line: TStringList;
  4.   Row, Col: integer;
  5.   C_FNAME: string;
  6. begin
  7.   grd_list.RowCount := 0;//clear any previous data
  8.  
  9.   TextFile := TStringList.Create;
  10.   try
  11.     Line := TStringList.Create;
  12.     try
  13.       Line.Delimiter := ' ';
  14.       TextFile.LoadFromFile(C_FNAME);
  15.  
  16.       grd_list.RowCount := TextFile.Count+1;
  17.       for Row := 1 to TextFile.Count do
  18.       begin
  19.         Line.DelimitedText := TextFile[Row-1];
  20.  
  21.         for Col := 1 to grd_list.ColCount-1 do
  22.           if Col <= Line.Count then
  23.             grd_list.Cells[Col, Row] := Line[Col-1]
  24.           else
  25.             grd_list.Cells[Col, Row] := 'N/A';
  26.  
  27.       end;
  28.  
  29.     finally
  30.       Line.Free;
  31.     end;
  32.   finally
  33.     TextFile.Free;
  34.   end;
  35.  
  36. end;

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: StringGrid lock fixed column
« Reply #4 on: September 14, 2016, 12:36:09 pm »
You're actually reinventing the wheel, since TStringGrid already has all this functionality already provided (with more than one API call to choose from).
A simple call to
Code: Pascal  [Select][+][-]
  1. StringGrid1.LoadFromCSVFile('testgrid.txt');
is all that is required.
See the example testgrid.txt file attached.

 

TinyPortal © 2005-2018