Recent

Author Topic: [SOLVED] StringGrid how to add a column by code?  (Read 279 times)

What I can do

  • Full Member
  • ***
  • Posts: 118
[SOLVED] StringGrid how to add a column by code?
« on: October 06, 2024, 10:18:13 pm »
OS:Windows 10@64
CODE: FPC-Lazarus 3.4
Project: Add a column by code
Code: Pascal  [Select][+][-]
  1. //---PrintGrid:TStringGrid
  2.  PrintGrid.InsertColRow(TRUE,PrintGrid.ColCount);
  3.  PrintGrid.Columns[PrintGrid.ColCount-1].Alignment:=taCenter;

I'm missing something and have tried everything I can think of
PrintGrid.Columns.Add;              failed
PrintGrid.InsertColRow(TRUE,0); failed
the presets are FixedCol=0, FixedRow:=10;

no columns have been added with the StringGrid column editing tools.
To me it's like the column gets created temporary but Grid dose not register the new column and any columns[aCol] parameter will produce invalid index exception
Even tried PrintGrid.Invalidate after InsertColRow but that didn't help.

UPDATE...... If I use the StringGrid IDE column tools to add a column then everything works but I just don't know how to do that with code.


« Last Edit: October 06, 2024, 11:24:54 pm by What I can do »

jamie

  • Hero Member
  • *****
  • Posts: 6695
Re: StringGrid how to add a column by code?
« Reply #1 on: October 06, 2024, 11:15:05 pm »
Colums.Add returns a new TGridColumn from which you fill in the details.

Also, the new column needs the width set.

Code: Pascal  [Select][+][-]
  1. MyGridColumn := Grid.Columns.Add;
  2. MyGridColumn.width := 100;
  3. etc
  4.  

Something on the lines of that.
The only true wisdom is knowing you know nothing

What I can do

  • Full Member
  • ***
  • Posts: 118
Re: StringGrid how to add a column by code?
« Reply #2 on: October 06, 2024, 11:24:29 pm »
OK, I found a work around.
NOTE: a new tStringGrid dropped on a form has a default of 5 columns and 5 rows.
Yes you can set the ColCount to 0 and even the RowCount to 0
However once I use StringGrid column tools and create a column (not by code, code must have at least one colcount) then one column gets created and ColCount is auto set to 1.
Once this is created then it can NOT be easily deleted without error flags of incorrect settings.
So inconclusion, At least one colcount and column must exist at all times.
If you set the colcount to 0 at first time placed on the form then index errors will occur. 

What I can do

  • Full Member
  • ***
  • Posts: 118
Re: [SOLVED] StringGrid how to add a column by code?
« Reply #3 on: October 06, 2024, 11:36:32 pm »
Thanks for the replay, jamie
Yes I created a Record of TColumns parameters
so I have a READ, EDIT, POST routines that deal with the Row record accordingly.

Code: Pascal  [Select][+][-]
  1.  FStrinGridCol=Record
  2.                  TA:TAlignMent;
  3.                  TS:TFontStyles; //[fsBold]
  4.                  TT:string;
  5.                  W:integer;
  6.                  A:TAlignment;
  7.                  c:TColor;  //TColor
  8.                  Fc:TColor; //TColor
  9.                  FSz:Integer;
  10.                  FSy:TFontStyles;//[stBold]
  11. ///---------------Procedure
  12. Procedure TForm1.AddStringGridCol(aGrid:TStringGrid;aSG:FStrinGridCol;aCol:Integer);
  13. begin
  14.  aGrid.InsertColRow(TRUE,aCol);
  15.  aGrid.Columns[aCol].Title.Alignment:=aSG.TA;  //SG.TA //TAlignMent
  16.  aGrid.Columns[aCol].Title.Font.Style:=aSG.TS;  //SG.TS //[fsBold]
  17.  aGrid.Columns[aCol].Title.Caption:=aSG.TT; //SG.TT //string
  18.  aGrid.Columns[aCol].Width:=aSG.W;             //SG.W  //integer
  19.  aGrid.Columns[aCol].Alignment:=aSG.A;         //SG.A  //alingment
  20.  aGrid.Columns[aCol].Color:=aSG.c;             //SG.c  //TColor
  21.  aGrid.Columns[aCol].Font.Color:=aSG.Fc;                //SG.Fc //TColor
  22.  aGrid.Columns[aCol].Font.Size:=aSG.FSz;                 //SG.FSz//Integer
  23.  aGrid.Columns[aCol].Font.Style:=SG.FSy;              //SG.FSy//[stBold
  24. end;
  25.  
« Last Edit: October 06, 2024, 11:40:51 pm by What I can do »

bobby100

  • Sr. Member
  • ****
  • Posts: 260
    • Malzilla
Re: StringGrid how to add a column by code?
« Reply #4 on: October 06, 2024, 11:51:19 pm »
OK, I found a work around.
NOTE: a new tStringGrid dropped on a form has a default of 5 columns and 5 rows.
Yes you can set the ColCount to 0 and even the RowCount to 0
However once I use StringGrid column tools and create a column (not by code, code must have at least one colcount) then one column gets created and ColCount is auto set to 1.
Once this is created then it can NOT be easily deleted without error flags of incorrect settings.
So inconclusion, At least one colcount and column must exist at all times.
If you set the colcount to 0 at first time placed on the form then index errors will occur.
Do you have a fixed column in place? The fixed columns and rows are also counted in ColCount/RowCount. You can't se ColCount := 0 if you have a fixed column.

What I can do

  • Full Member
  • ***
  • Posts: 118
Re: [SOLVED] StringGrid how to add a column by code?
« Reply #5 on: October 06, 2024, 11:57:29 pm »
Both FIX- Column and Rows are set to 0, and ColCount set to 1, RowCount set to 10
               

 

TinyPortal © 2005-2018