Recent

Author Topic: Project raised exception class 'EGridException'  (Read 2663 times)

AfootDesert41

  • New Member
  • *
  • Posts: 15
Project raised exception class 'EGridException'
« on: December 30, 2018, 04:34:29 am »
I got this message while I was testing a project I'm working on. This is the full message of the window:

[Debugger Exception Notification]

Project project1 raised exception class 'EGridException' with message:
Index Out of range Cell[Col=1 Row=2]

In file 'grids.pas' at line 9617

I am trying to code a dynamic TStringGrid which is going to add a row when I add a new 'item' and add the data of that 'item' in each column, I really have no idea of what's going on as I'm very new on using lazarus and developing GUI's. I'm gonna leave the code below:

Main Unit code:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   Grids,Unit2,Unit3,Unit4,Unit5;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     botonborrarcliente: TButton;
  17.     botonagregarcliente: TButton;
  18.     Button1: TButton;
  19.     Button2: TButton;
  20.     botonborrarproductomain: TButton;
  21.     botonagregarproductomain: TButton;
  22.     botonfacturar: TButton;
  23.     subtotalbarra: TEdit;
  24.     ivabarra: TEdit;
  25.     totalbarra: TEdit;
  26.     GroupBox3: TGroupBox;
  27.     Label6: TLabel;
  28.     Label7: TLabel;
  29.     Label8: TLabel;
  30.     StringGrid1: TStringGrid;
  31.     usuariocajerobarra: TEdit;
  32.     GroupBox2: TGroupBox;
  33.     Label5: TLabel;
  34.     nombrerazonsocialbarra: TEdit;
  35.     apellidobarra: TEdit;
  36.     cedularifbarra: TEdit;
  37.     direccionbarra: TEdit;
  38.     GroupBox1: TGroupBox;
  39.     Label1: TLabel;
  40.     Label2: TLabel;
  41.     Label3: TLabel;
  42.     Label4: TLabel;
  43.     procedure botonagregarclienteClick(Sender: TObject);
  44.     procedure botonagregarproductomainClick(Sender: TObject);
  45.     procedure botonborrarclienteClick(Sender: TObject);
  46.     procedure botonfacturarClick(Sender: TObject);
  47.     procedure Button1Click(Sender: TObject);
  48.     procedure Button2Click(Sender: TObject);
  49.   private
  50.  
  51.   public
  52.   nombreorazonsocial_ing,apellido_ing,cedulaorif_ing,direccion_ing,
  53.   usuariocajero_ing,formadepago_ing,codigoproducto_ing,
  54.   descripcionproducto_ing:String;
  55.   cantidadproducto_ing:integer;
  56.   costeproducto_ing:real;
  57.   end;
  58.  
  59. var
  60.   Form1: TForm1;
  61.  
  62. implementation
  63.  
  64. {$R *.lfm}
  65.  
  66. { TForm1 }
  67.  
  68. procedure TForm1.botonagregarproductomainClick(Sender: TObject);
  69. begin
  70.   form2.showmodal;
  71. end;
  72.  
  73. procedure TForm1.botonborrarclienteClick(Sender: TObject);
  74. begin
  75.   nombreorazonsocial_ing:=#0;
  76.   nombrerazonsocialbarra.ReadOnly:=False;
  77.   nombrerazonsocialbarra.Text:='';
  78.   apellido_ing:=#0;
  79.   apellidobarra.ReadOnly:=False;
  80.   apellidobarra.Text:='';
  81.   cedulaorif_ing:=#0;
  82.   cedularifbarra.ReadOnly:=False;
  83.   cedularifbarra.Text:='';
  84.   direccion_ing:=#0;
  85.   direccionbarra.ReadOnly:=False;
  86.   direccionbarra.Text:='';
  87. end;
  88.  
  89. procedure TForm1.botonagregarclienteClick(Sender: TObject);
  90. begin
  91.   nombreorazonsocial_ing:=nombrerazonsocialbarra.text;
  92.   nombrerazonsocialbarra.ReadOnly:=True;
  93.   apellido_ing:=apellidobarra.Text;
  94.   apellidobarra.ReadOnly:=True;
  95.   cedulaorif_ing:=cedularifbarra.Text;
  96.   cedularifbarra.ReadOnly:=True;
  97.   direccion_ing:=direccionbarra.Text;
  98.   direccionbarra.readonly:=True;
  99. end;
  100.  
  101. procedure TForm1.botonfacturarClick(Sender: TObject);
  102. begin
  103.   form4.showmodal;
  104. end;
  105.  
  106. procedure TForm1.Button1Click(Sender: TObject);
  107. begin
  108.   form3.showmodal;
  109. end;
  110.  
  111. procedure TForm1.Button2Click(Sender: TObject);
  112. begin
  113.   usuariocajero_ing:=#0;
  114.   usuariocajerobarra.text:='';
  115. end;
  116. end.
  117.  

Unit2 code (where the code of the grid is):

Code: Pascal  [Select][+][-]
  1. unit Unit2;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm2 }
  13.  
  14.   TForm2 = class(TForm)
  15.     botonagregarbroductounit2: TButton;
  16.     botoncancelarunit2: TButton;
  17.     codigobarra: TEdit;
  18.     descripcionbarra: TEdit;
  19.     cantidadbarra: TEdit;
  20.     costeunitariobarra: TEdit;
  21.     GroupBox1: TGroupBox;
  22.     Label1: TLabel;
  23.     Label2: TLabel;
  24.     Label3: TLabel;
  25.     Label4: TLabel;
  26.     procedure botonagregarbroductounit2Click(Sender: TObject);
  27.     procedure botoncancelarunit2Click(Sender: TObject);
  28.   private
  29.  
  30.   public
  31.   i:integer;
  32.   end;
  33.  
  34. var
  35.   Form2: TForm2;
  36.  
  37. implementation
  38. uses Unit1;
  39.  
  40. {$R *.lfm}
  41.  
  42. { TForm2 }
  43.  
  44. procedure TForm2.botonagregarbroductounit2Click(Sender: TObject);
  45. begin
  46.   i:=0;
  47.   form1.descripcionproducto_ing:=descripcionbarra.Text;
  48.   form1.codigoproducto_ing:=codigobarra.Text;
  49.   form1.cantidadproducto_ing:=StrToInt(cantidadbarra.Text);
  50.   form1.costeproducto_ing:=StrToInt(costeunitariobarra.Text);
  51.   descripcionbarra.Text:='';
  52.   codigobarra.Text:='';
  53.   cantidadbarra.Text:='';
  54.   costeunitariobarra.Text:='';
  55.   Form2.Close;
  56.  
  57.   i:=i+1;
  58.  
  59.      with Form1.StringGrid1 do
  60.        RowCount:=i;
  61.        Form1.StringGrid1.Cells [ 1, i ] := Form1.codigoproducto_ing;
  62.        Form1.StringGrid1.Cells [ 2, i ] := Form1.descripcionproducto_ing;
  63.        Form1.StringGrid1.Cells [ 3, i ] := IntToStr(Form1.cantidadproducto_ing);
  64.        Form1.StringGrid1.Cells [ 4, i ] := FloatToStr(Form1.costeproducto_ing);
  65.  
  66. end;
  67.  
  68. procedure TForm2.botoncancelarunit2Click(Sender: TObject);
  69. begin
  70.   form2.close;
  71. end;
  72.  
  73. end.
  74.  

VTwin

  • Hero Member
  • *****
  • Posts: 1227
  • Former Turbo Pascal 3 user
Re: Project raised exception class 'EGridException'
« Reply #1 on: December 30, 2018, 05:48:13 am »
The stringgrid does not have enough rows or columns allocated. It might help if you post the full project, the lfm files contain information on the stringgrid's initial state.

One question is does the stringgrid have fixed rows or columns. Another is how many columns does it have.
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 15.3.2: Lazarus 3.8 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 3.8 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 3.8 (64 bit on VBox)

dbannon

  • Hero Member
  • *****
  • Posts: 3374
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Project raised exception class 'EGridException'
« Reply #2 on: December 30, 2018, 09:11:29 am »
Yep, VTwin is almost certainly right. Better to 'insert' content - something along these lines -

Code: Pascal  [Select][+][-]
  1.        
  2.     Grid.Clear;
  3.     Grid.FixedRows := 0;
  4.     Grid.InsertRowWithValues(0, ['Title', 'Last Change', 'Create Date', 'File Name']);
  5.     Grid.FixedRows := 1;
  6.         for Index := 0 to NoteList.Count -1 do begin
  7.         Grid.InsertRowWithValues(Index+1, [NoteList.Items[Index]^.Title,
  8.                 NoteList.Items[Index]^.LastChange, NoteList.Items[Index]^.CreateDate,
  9.                 NoteList.Items[Index]^.ID]);
  10.         end;
  11.  

(copy and paste from my code, don't worry about the identifier name, you get the idea. 'Grid' is a TStringGrid.)

Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

jamie

  • Hero Member
  • *****
  • Posts: 6953
Re: Project raised exception class 'EGridException'
« Reply #3 on: December 30, 2018, 02:49:32 pm »
one important aspect that you are not telling the NEW coder to this language is the CELLS are
zero based indexed.
 
 So if you have lets say 10 rows, the highest index that can be used for a ROW is 9

 so this means 0..9, and all related index are the same.

 so if you have COLS count of 10, you can only index up to 9..

something to remember..
The only true wisdom is knowing you know nothing

AfootDesert41

  • New Member
  • *
  • Posts: 15
Re: Project raised exception class 'EGridException'
« Reply #4 on: December 30, 2018, 05:57:46 pm »
Thank you everyone for the advices, I solved the problem :D

 

TinyPortal © 2005-2018