Recent

Author Topic: [SOLVED] StringGrid out of range errors  (Read 18118 times)

guest48180

  • Guest
[SOLVED] StringGrid out of range errors
« on: November 26, 2013, 01:38:49 am »
I have 2 stringgrids. The first grid populates without error, but the second grid keeps giving out of range errors. I populate the grids with titles at runtime, then set both grids to 1 row. I then display any data that the textfiles might have with the call to DisplayData. sg1 populates fine, as shld sg2...but it fails. The increment line is set for sg1, but both grids are using it since both are now set to first row.

sg1 is the first grid
sg2 is the second grid

Error: Index out of range Cell[Col=1 Row=1

I can comment out the sg2 lines one at a time, and the error just  moves to the next line and throws an error for that line. I have both grid rows set to 1 before this code executes. Again, sg1 populates fine. The issue is sg2.

Code: [Select]
procedure TForm1.DisplayData;
var
  F: TextFile;
  rowIndex: integer=1;
  s: ShortString;

  procedure FillGrid(aRowIndex: integer);
  begin
    if aRowIndex>sg1.RowCount-1 then
      sg1.RowCount:=aRowIndex+1;
    sg1.Cells[1,aRowIndex]:= CustRec.RepeatCust;
    sg1.Cells[2,aRowIndex]:= IntToStr(CustRec.CustNum);
    sg1.Cells[3,aRowIndex]:= IntToStr(CustRec.OrderNum);
    sg1.Cells[4,aRowIndex]:= CustRec.LastName;
    sg1.Cells[5,aRowIndex]:= CustRec.FirstName;
    sg1.Cells[6,aRowIndex]:= CustRec.Address;
    sg1.Cells[7,aRowIndex]:= CustRec.City;
    sg1.Cells[8,aRowIndex]:= CustRec.State;
    sg1.Cells[9,aRowIndex]:= CustRec.Zip;
    sg1.Cells[10,aRowIndex]:= CustRec.Email1;
    sg1.Cells[11,aRowIndex]:= CustRec.Email2;
    sg1.Cells[12,aRowIndex]:= CustRec.HomeNum;
    sg1.Cells[13,aRowIndex]:= CustRec.CellNum;
    sg1.Cells[14,aRowIndex]:= CustRec.WorkNum;
    sg1.Cells[15,aRowIndex]:= CustRec.Ext;
    sg2.Cells[1,aRowIndex]:= IntToStr(CustRec.CustNum);
    sg2.Cells[2,aRowIndex]:= IntToStr(CustRec.OrderNum);
    sg2.Cells[3,aRowIndex]:= CustRec.I]"]>Blockedrdered;
    sg2.Cells[4,aRowIndex]:= IntToStr(CustRec.Quantity);
    sg2.Cells[5,aRowIndex]:= IntToStr(CustRec.Cost);
    sg2.Cells[6,aRowIndex]:= IntToStr(CustRec.Tax);
    sg2.Cells[7,aRowIndex]:= IntToStr(CustRec.TotalCost);
    sg2.Cells[8,aRowIndex]:= CustRec.MaterialNeeded;
    sg2.Cells[9,aRowIndex]:= CustRec.SpecialNotes;
  end;

begin
  AssignFile(F, 'gridFile.dat');
  try
    Reset(F);
    while not EOF(F) do begin
      readln(F, CustRec.RepeatCust);
      readln(F, s);
      CustRec.CustNum:=StrToInt(s);
      ReadLn(F, s);
      CustRec.OrderNum:=StrToInt(s);
      readln(F, CustRec.LastName);
      readln(F, CustRec.FirstName);
      readln(F, CustRec.Address);
      readln(F, CustRec.City);
      readln(F, CustRec.State);
      readln(F, CustRec.Zip);
      readln(F, CustRec.Email1);
      readln(F, CustRec.Email2);
      readln(F, CustRec.HomeNum);
      readln(F, CustRec.CellNum);
      readln(F, CustRec.WorkNum);
      readln(F, CustRec.Ext);
      readln(F, CustRec.I]"]>Blockedrdered);
      readln(F,s);
      CustRec.Quantity:= StrToInt(s);
      readln(F,s);
      CustRec.Cost:= StrToInt(s);
      readln(F,s);
      CustRec.Tax:= StrToInt(s);
      readln(F,s);
      CustRec.TotalCost:= StrToInt(s);
      readln(F, CustRec.MaterialNeeded);
      readln(F, CustRec.SpecialNotes);
      FillGrid(rowIndex);
      Inc(rowIndex);
    end;
  finally
    CloseFile(F);
  end;
end;

Any insight wld be appreciated.

Landslyde

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: StringGrid out of range errors
« Reply #1 on: November 26, 2013, 02:18:21 am »
Are you setting rowcount for sg2? It looks like you made the resize condition only for sg1.

guest48180

  • Guest
Re: StringGrid out of range errors
« Reply #2 on: November 26, 2013, 02:31:17 am »
Quote
Are you setting rowcount for sg2? It looks like you made the resize condition only for sg1.

Once again, I shot myself in the foot. I guess I need to lay off the Qualudes  :D
Thanks  :)

guest48180

  • Guest
Re: StringGrid out of range errors
« Reply #3 on: November 26, 2013, 02:38:57 am »
Well, no. I didn't set an increment for sg2 because both grids are set to 1 row at runtime. And since both are just having the title populated, I didn't see the need to do anything other to the code. One works for the other, in this case. And I just made two routines to test your suggestion and got the same result. So that's not the ticket. But thanks for your help, User137

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: StringGrid out of range errors
« Reply #4 on: November 26, 2013, 03:05:27 am »
Landslyde, this needed code you wrote for sg1:

Code: [Select]
    if aRowIndex>sg1.RowCount-1 then
      sg1.RowCount:=aRowIndex+1;

is also needed for sg2. Add it:

Code: [Select]
    if aRowIndex > sg2.RowCount - 1 then
      sg2.RowCount := aRowIndex + 1;

because your procedure gets called as much as there are records in your file. Like this:

Code: [Select]
...
    while not EOF(F) do begin
...
      FillGrid(rowIndex);
      Inc(rowIndex);
    end;

Which means rowIndex keeps increasing. Right?

guest48180

  • Guest
Re: StringGrid out of range errors
« Reply #5 on: November 26, 2013, 03:38:06 am »
Quote
because your procedure gets called as much as there are records in your file. Like this:

Code: [Select]
...
    while not EOF(F) do begin
...
      FillGrid(rowIndex);
      Inc(rowIndex);
    end;

Which means rowIndex keeps increasing. Right?

No, engkin. I am calling data from a single file, and it has just one record to read. I just separate it on two grids. Customer number, address, city and the like I put on sg1. Then I put customer number, order number, what was ordered, costs, etc on sg2. It's from a single text file. So eof is eof for both sg1 and sg2. And that's what has me puzzled. Data's put on sg1, but then when it gets to sg2 it starts throwing errors. I can comment sg2 out line by line, and the errors follow the code left for sg2 to populate. So I'm baffled about the fact that it throws errors on sg2 and not sg1. Do I make sense? I tend to ramble on, you see  :D

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: StringGrid out of range errors
« Reply #6 on: November 26, 2013, 04:21:05 am »
You are right.  :-[ I did not pay enough attention to the error message:

Quote
Index out of range Cell[Col=1 Row=1

So when you comment sg2 out line by line, the Col number in the error message increases as well, right?

If so, double check both RowCount and ColCount of sg2

Edit:

As a hint, FixedRows can cause that error if RowCount - FixedRows < aRowIndex. That also applies to FixedCols.

As an example, if FixedRows = 1 and RowCount = 1, you will get that error when you use Cell[1,1].
« Last Edit: November 26, 2013, 04:33:21 am by engkin »

ausdigi

  • Jr. Member
  • **
  • Posts: 52
  • Programmer
    • RNR
Re: StringGrid out of range errors
« Reply #7 on: November 26, 2013, 04:33:45 am »
Just reiterating/reinforcing.

Landslyde: You are NOT settings sg2.RowCount in your original code and your error message says you are trying to set row index 1 to a grid you later state is "set to 1 row at runtime", which is row index 0 (zero). [The code you posted does not show sg2.RowCount being set at runtime.] You haven't said if you have implemented the code suggested by User137 and shown by engkin.
Win10/64: CT 5.7 32&64

guest48180

  • Guest
Re: StringGrid out of range errors
« Reply #8 on: November 26, 2013, 04:35:37 am »
Quote
So when you comment sg2 out line by line, the Col number in the error message increases as well, right?

Without commenting out, I get the error on Col 1 Row 1. I comment out the first line and the error is Col 2 Row 1, and so on. I don't understand. The spark plugs are tight and have the wires on them...but it won't run for me. You see, it's this sort of stuff that drives me crazy. I've been working on this since last night with nothing new to bring to the table. I don't see anything wrong with the code.

guest48180

  • Guest
Re: StringGrid out of range errors
« Reply #9 on: November 26, 2013, 04:38:28 am »
Quote
Landslyde: You are NOT settings sg2.RowCount in your original code and your error message says you are trying to set row index 1 to a grid you later state is "set to 1 row at runtime", which is row index 0 (zero). [The code you posted does not show sg2.RowCount being set at runtime.] You haven't said if you have implemented the code suggested by User137 and shown by engkin.

Here is the updated code...and it STILL doesn't work.

Code: [Select]
procedure TForm1.DisplayData;
var
  F: TextFile;
  rowIndex: integer=1;
  s: ShortString;

  procedure FillGrid(aRowIndex: integer);
  begin
     begin
       sg1.RowCount:= 1;
        if aRowIndex>sg1.RowCount-1 then
      sg1.RowCount:=aRowIndex+1;
    sg1.Cells[1,aRowIndex]:= CustRec.RepeatCust;
    sg1.Cells[2,aRowIndex]:= IntToStr(CustRec.CustNum);
    sg1.Cells[3,aRowIndex]:= IntToStr(CustRec.OrderNum);
    sg1.Cells[4,aRowIndex]:= CustRec.LastName;
    sg1.Cells[5,aRowIndex]:= CustRec.FirstName;
    sg1.Cells[6,aRowIndex]:= CustRec.Address;
    sg1.Cells[7,aRowIndex]:= CustRec.City;
    sg1.Cells[8,aRowIndex]:= CustRec.State;
    sg1.Cells[9,aRowIndex]:= CustRec.Zip;
    sg1.Cells[10,aRowIndex]:= CustRec.Email1;
    sg1.Cells[11,aRowIndex]:= CustRec.Email2;
    sg1.Cells[12,aRowIndex]:= CustRec.HomeNum;
    sg1.Cells[13,aRowIndex]:= CustRec.CellNum;
    sg1.Cells[14,aRowIndex]:= CustRec.WorkNum;
    sg1.Cells[15,aRowIndex]:= CustRec.Ext;
     end;

  begin
     sg2.RowCount:= 1;
     if aRowIndex>sg2.RowCount-1 then
      sg2.RowCount:=aRowIndex+1;
    sg2.Cells[1,aRowIndex]:= IntToStr(CustRec.CustNum);
    sg2.Cells[2,aRowIndex]:= IntToStr(CustRec.OrderNum);
    sg2.Cells[3,aRowIndex]:= CustRec.I]"]>Blockedrdered;
    sg2.Cells[4,aRowIndex]:= IntToStr(CustRec.Quantity);
    sg2.Cells[5,aRowIndex]:= IntToStr(CustRec.Cost);
    sg2.Cells[6,aRowIndex]:= IntToStr(CustRec.Tax);
    sg2.Cells[7,aRowIndex]:= IntToStr(CustRec.TotalCost);
    sg2.Cells[8,aRowIndex]:= CustRec.MaterialNeeded;
    sg2.Cells[9,aRowIndex]:= CustRec.SpecialNotes;
  end;
  end;


begin
  AssignFile(F, 'gridFile.dat');
  try
    Reset(F);
    while not EOF(F) do begin
      readln(F, CustRec.RepeatCust);
      readln(F, s);
      CustRec.CustNum:=StrToInt(s);
      ReadLn(F, s);
      CustRec.OrderNum:=StrToInt(s);
      readln(F, CustRec.LastName);
      readln(F, CustRec.FirstName);
      readln(F, CustRec.Address);
      readln(F, CustRec.City);
      readln(F, CustRec.State);
      readln(F, CustRec.Zip);
      readln(F, CustRec.Email1);
      readln(F, CustRec.Email2);
      readln(F, CustRec.HomeNum);
      readln(F, CustRec.CellNum);
      readln(F, CustRec.WorkNum);
      readln(F, CustRec.Ext);
      readln(F, CustRec.I]"]>Blockedrdered);
      readln(F,s);
      CustRec.Quantity:= StrToInt(s);
      readln(F,s);
      CustRec.Cost:= StrToInt(s);
      readln(F,s);
      CustRec.Tax:= StrToInt(s);
      readln(F,s);
      CustRec.TotalCost:= StrToInt(s);
      readln(F, CustRec.MaterialNeeded);
      readln(F, CustRec.SpecialNotes);
      FillGrid(rowIndex);
      Inc(rowIndex);
    end;
  finally
    CloseFile(F);
  end;
end; 

Now what?

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: StringGrid out of range errors
« Reply #10 on: November 26, 2013, 04:43:16 am »
Before using sg2, add this line:

Code: [Select]
  sg2.RowCount := sg2.FixedRows + 1;// 1 is the number of records
« Last Edit: November 26, 2013, 04:45:07 am by engkin »

guest48180

  • Guest
Re: StringGrid out of range errors
« Reply #11 on: November 26, 2013, 04:48:08 am »
Quote
As an example, if FixedRows = 1 and RowCount = 1, you will get that error when you use Cell[1,1].

I did not know that. That's the way it is, FixedRow:= 1 and RowCount:= 1. But I have that for both grids, sg1 and sg2. And sg1 runs fine, slides like silk. I will sit back and take a harder look at everything. I f I find something, I'll certainly let you know. Thanks for your help.

guest48180

  • Guest
Re: StringGrid out of range errors
« Reply #12 on: November 26, 2013, 04:55:20 am »
Quote
Before using sg2, add this line:

Code: [Select]
  sg2.RowCount := sg2.FixedRows + 1;// 1 is the number of records

That didn't fix it, engkin. I tried adding another record to the file. Sg1 works, but sg2 crashed. I have both grids setup to mirror one another. One works and one doesn't. This is the kind of thing that takes discouragement to a whole new level. That's for trying to help me though. I do appreciate you for that.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: [SOLVED] StringGrid out of range errors
« Reply #13 on: November 26, 2013, 05:51:40 am »
You did not pay attention to this comment:

Quote
// 1 is the number of records

In this case it should be:

Code: [Select]
sg2.RowCount := sg2.FixedRows + aRowIndex ;
or

Code: [Select]
  if aRowIndex > (sg2.RowCount - sg2.FixedRows) then
    sg2.RowCount := sg2.FixedRows + aRowIndex;

Don't get discouraged.  ;D

WickedDum

  • Full Member
  • ***
  • Posts: 211
Re: [SOLVED] StringGrid out of range errors
« Reply #14 on: November 27, 2013, 12:59:38 am »
You can nest Begin/Ends, but can you use them in succession?

Here is the current structure posted:
Code: [Select]
procedure FillGrid(aRowIndex: integer);
  begin
     begin
     end;
     begin
     end;
  end;


Just a thought...
Practice Safe Computing!!

Intel i5-4460K @ 3.2GHz | Win8.1 64-bit | FPC: v3.0 | Lazarus:  v1.6.0

 

TinyPortal © 2005-2018