Recent

Author Topic: Visible columns in TStringGrid (SOLVED)  (Read 16754 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
Visible columns in TStringGrid (SOLVED)
« on: June 20, 2011, 05:37:39 pm »
Why in my code, thi s line create this error: List index(0) out of bound

    Self.Columns[ColIndex].Visible:=Value;

Code: [Select]
unit ExtDBListBox;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Grids, sqldb, db;

type
  TExtDBListBox = class(TStringGrid)
  private
    { Private declarations }
    MyDataSource: TDataSource;     //Self.MyDataSource.OnDataChange
  protected
    { Protected declarations }
  public
    { Public declarations }
    constructor Create(TheOwner: TComponent); override;
    procedure Requery();
    procedure ColumnVisible(ColIndex: integer; Value: boolean);
    procedure ColumnAlign(ColIndex: integer; Value: integer);
  published
    { Published declarations }
    procedure SetDataSource(AppDataSource: TDataSource);
  end;

procedure Register;

implementation

procedure Register;
begin
  {$I ExtDBListBox_icon.lrs}
  RegisterComponents('Standard',[TExtDBListBox]);
end;

constructor TExtDBListBox.Create(TheOwner: TComponent);
begin
     inherited Create(TheOwner);
     {cambio il layout della griglia per far si che si veda come se fosse una listbox}
     Self.ColCount:=1;
     Self.FixedCols:=0;
     Self.FixedRows:=0;
     Self.AutoEdit:=false;
     Self.GridLineWidth:=0;
end;

procedure TExtDBListBox.Requery();
var
   NumRighe, NumColonne: LongInt;
   Colonna, Riga: integer;
   app:string;
begin
//     Self.Clear; //pulisco la listbox
     {ora disegno il dataset}
     if not MyDataSource.DataSet.EOF then
     begin
          {mi ricavo il numero di colonne che devo stampare a video}
          NumColonne:=MyDataSource.DataSet.FieldCount;
          Self.ColCount:=NumColonne;
          {mi scorro velocemente il recordset per capire quante righe ho}
          MyDataSource.DataSet.First;
          NumRighe:=0;
          while not MyDataSource.DataSet.EOF do
          begin
               Inc(NumRighe);
               MyDataSource.DataSet.Next;
          end;
          {ora che ho il numero di righe mi stampo la griglia}
          Self.RowCount:=NumRighe+1;
          {ora inserisco la testata}
          for Colonna:=0 to NumColonne-1 do
          begin
               app:=MyDataSource.DataSet.Fields[Colonna].Name;
               if Length(Trim(app))<=0 then
                  app:='Col' + IntToStr(Colonna);
               Self.Cells[Colonna,0]:=app;
          end;
          {ora inserisco i dati}
          Riga:=1;
          MyDataSource.DataSet.First;
          while not MyDataSource.DataSet.EOF do
          begin
               {disegno la singola riga}
               for Colonna:=0 to NumColonne-1 do
               begin
                    Self.Cells[Colonna,Riga]:=MyDataSource.DataSet.Fields[Colonna].AsString;
               end;
               Inc(Riga);
               MyDataSource.DataSet.Next;
          end;
     end;
     Self.AutoSizeColumns; //dimensiona le colonne correttamente
end;

procedure TExtDBListBox.ColumnVisible(ColIndex: integer; Value: boolean);
begin
     Self.Columns[ColIndex].Visible:=Value;
end;

procedure TExtDBListBox.ColumnAlign(ColIndex: integer; Value: integer);
begin
     if Value<0 then
        Self.Columns[ColIndex].Alignment:=taLeftJustify
     else if Value=0 then
          Self.Columns[ColIndex].Alignment:=taCenter
     else
         Self.Columns[ColIndex].Alignment:=taRightJustify;
end;

procedure TExtDBListBox.SetDataSource(AppDataSource: TDataSource);
begin
     Self.MyDataSource:=AppDataSource;
end;

end.
« Last Edit: June 21, 2011, 04:52:58 pm by xinyiman »
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Visible columns in TStringGrid
« Reply #1 on: June 20, 2011, 05:54:33 pm »
I can replicate this.

Lazarus 0.9.30 r29749 FPC 2.4.2 i386-win32-win32/win64

However, if you create the columns via Columns property on Object Inspector, this does not occur.
« Last Edit: June 20, 2011, 06:04:32 pm by typo »

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Visible columns in TStringGrid
« Reply #2 on: June 20, 2011, 08:18:35 pm »
Quote
Why in my code, thi s line create this error: List index(0) out of bound
    Self.Columns[ColIndex].Visible:=Value;

Because you didn't create any Column.
There are two ways how to work with TStringGrid:
 1) you can set simply set ColCount
 2) you can define Columns
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/

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
Re: Visible columns in TStringGrid
« Reply #3 on: June 21, 2011, 07:44:45 am »
In my case I suggest we proceed? Perhaps with an example
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Visible columns in TStringGrid
« Reply #4 on: June 21, 2011, 04:06:40 pm »
Simply:
Code: [Select]
StringGrid1.Columns.Add;
Once Columns are defined (i.e. Columns.Count>0) then StringGrid.ColCount property in fact become readonly. When you try set it, you will get debugger error.
Similary, you can do:
Code: [Select]
StringGrid1.Columns.Delete(i);
StringGrid1.Columns.Insert(i);
StringGrid1.Columns.Exchange(i, j);

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/

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
Re: Visible columns in TStringGrid
« Reply #5 on: June 21, 2011, 04:52:46 pm »
Thank you very much
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
Re: Visible columns in TStringGrid (SOLVED)
« Reply #6 on: June 23, 2011, 02:23:56 pm »
Ok I managed to hide columns. And if I want to align the content of the columns as I do? So me error:

procedure TExtDBListBox.ColumnVisible(ColIndex: integer; Value: boolean);
begin
     Self.Columns[ColIndex].Visible:=Value;
end; 
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Visible columns in TStringGrid (SOLVED)
« Reply #7 on: June 23, 2011, 02:47:50 pm »
Code: [Select]
procedure TExtDBListBox.ColumnVisible(ColIndex: integer; Value: boolean);
begin
     Self.Columns[ColIndex].Visible:=Value;
end;

If this gives error then you are out of range or you did not create any columns.
If you have 10 columns then they are indexed from 0..9 .
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/

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
Re: Visible columns in TStringGrid (SOLVED)
« Reply #8 on: June 23, 2011, 02:57:46 pm »
Sorry, is this code when produce error

procedure TExtDBListBox.ColumnAlign(ColIndex: integer; Value: TAlignment);
begin
     Self.Columns[ColIndex].Alignment:=Value;
end;         
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Visible columns in TStringGrid (SOLVED)
« Reply #9 on: June 23, 2011, 03:52:37 pm »
Well, Alignment is only alingment of text in cells, it works well here. I still think you are out of range as I wrote above. BTW, what error it gives?
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/

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
Re: Visible columns in TStringGrid (SOLVED)
« Reply #10 on: June 23, 2011, 03:57:41 pm »
ExtDBListBox1.ColumnAlign(0,taCenter);

No errors, just does not do what I say. Remains aligned to the left. And I have two columns.
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Visible columns in TStringGrid (SOLVED)
« Reply #11 on: June 23, 2011, 04:21:33 pm »
Do you have FixedCols=0?
If you have FixedRows>0 then you can try
Code: [Select]
procedure TExtDBListBox.ColumnAlign(ColIndex: integer; Value: TAlignment);
begin
     Self.Columns[ColIndex].Alignment:=Value;
     Self.Columns[ColIndex].Title.Alignment:=Value;
end;
to align both column and its title.
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/

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
Re: Visible columns in TStringGrid (SOLVED)
« Reply #12 on: June 23, 2011, 04:50:31 pm »
My layout grid

     Self.ColCount:=1;
     Self.FixedCols:=0;
     Self.FixedRows:=0;
     Self.AutoEdit:=false;
     Self.GridLineWidth:=0;     

No, even as you suggested works.
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Visible columns in TStringGrid (SOLVED)
« Reply #13 on: June 23, 2011, 04:59:04 pm »
Code: [Select]
Self.Columns[ColIndex].AlignmentIf this does not work for you then maybe it is some widgetset related bug.
Make some simple test project (with TStringGrid or with some component derived from TStringGrid) and put it to bugtracker.
I can only say that it works well here.
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/

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
Re: Visible columns in TStringGrid (SOLVED)
« Reply #14 on: June 23, 2011, 05:04:54 pm »
Ok, thank you
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

 

TinyPortal © 2005-2018