The Delphi Bug List

Entry No.
297
DB Controls - TDBGrid - DBGrids
When you connect a Grid to a datasource, it appears to randomly come up with the error "Grid Index Out of Range" when it is created.
Solution by Brian Wheatley.
Comment by Peter Disselkoen.
Solution by Morgan Martinet.
1.02 2.01 3.0 3.01 3.02 4.0 4.01 4.02 4.03 5.0 5.01 6.0 6.01 6.02 Kylix 1.0
FixedExistsUnknownUnknownUnknownUnknownUnknownUnknownUnknownUnknownUnknownUnknownUnknownUnknownN/A
Description
When you connect a Grid to a datasource, it appears to randomly come up with the error "Grid Index Out of Range" when it is created.
Solution by Brian Wheatley.
Comment by Peter Disselkoen.
Solution by Morgan Martinet.
Solution / workaround
By Brian Wheatley
Well, you said someone should look into this problem, and I did.
I had the problem when running a master/detail report with QuickReport, so I dug in.
The problem is in the file Grids.pas (c:\....\Delphi 2.0\VCL\Source), line 2427. The function is TCustomGrid.MoveCurrent, and the line reads:
  if (ACol < 0) or (ARow < 0) or
     (ACol >= ColCount) or (ARow >= RowCount) 
  then
    InvalidOp(SIndexOutOfRange);
I changed the >= in both places to just > and recompiled. The problem went away, but I'm not 100% certain what side effects this might have later on.

I (Reinier Sterkenburg) have my doubts about this solution because it is not logical: Because a StringGrid has ColCount colums (and its indexing is off-by-one), a value in the range [0..ColCount-1] is valid. So including ColCount as an accepted value doesn't make sense to me. But if this cures the "Grid Index Out of Range" problem, it might be useful to some people.
Peter Disselkoen wrote me (us) the following explanation:

The stated solution sent by Brian Wheatley is not correct.
When the error "Grid Index Out of Range" occurs, it means that you made a programming error somewhere else in your code. I applied the solution of Brian and received "Access violations" exceptions. By examing the code written, I came to the conclusion that somewhere in the code I forgot to disable the controls (TTable.DisableControls, see page 57 Database Application Developer's Guide Version 2). If the controls are not disabled and you are walking through the code and forget to return to a valid record, then this error (Grid Index out of Range) occurs. This error did not occur in version 1.0 of Delphi.
Patrick Maartense wrote in addition to that:

I read the solution to the index out of range problem with the DBGrids. I *do* think that this is the solution; I had the same problem MANY times, with M/D grids and it turned out that these were being rescaled while data was loaded. Now the rescaling doesn't happen anymore before the tables have been loaded and opened completely, the problem has gone. It appears that it does have something to do with the row position of the cursor.
Solution:
By Morgan Martinet

Instead of patching the method TCustomGrid.MoveCurrent, I patched the method TCustomDbGrid.UpdateActive (in unit DbGrids):
Instead of the line:
if Row <> NewRow then
I wrote:
if (Row <> NewRow) and (NewRow < RowCount) then
and now it works perfectly!
User-contributed comments
René Severens
10 Mar 2002  12:08 PM GMT
I tried the above solutions in D5 but it still doesn't work. What I did was:
Start a new App. Place a Dbgrid, a Table and a DataSource on the form, and hook them to DBDemos. Run the App and select the rightmost column by its title at the very right so you get a splitted arrowed drag&resize column cursor and drag outside of the form. The Column is resized nicely. Repeat this for two or three times using the scrollbar to eachtime place the rightmost side of the last coulmn into view. The error message will show up, again and again....
Latest update of this entry: before April 1998

Post a comment on this bug


Index page
Delphi Bug List home page
The Delphi Bug Lists are presently maintained by Jordan Russell, who has taken over this task from Reinier Sterkenburg since August 2000.
All feedback is appreciated. See also the feedback section of the Delphi Bug List home page.