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!
|