TStringGrid.DrawCell - Not Updating The Correct Cell



I have a problem that I've been trying to fix for a couple of days now
without success. I have a standard string grid with DefaultDrawing set
to False and it's double buffered. It has 1 column and 25 rows of which
15 are visible with no scroll bars. All the cells are painted black
with the exception of row 7 which is red. I want to keep this cell red
at all times so I have the following code in the DrawCell event:

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow:
Integer;
Rect: TRect; State: TGridDrawState);
begin
if ARow = StringGrid1.TopRow + 7 then
begin
StringGrid1.Canvas.Brush.Color := clRed;
StringGrid1.Canvas.FillRect(Rect);
end
else
begin
StringGrid1.Canvas.Brush.Color := clBlack;
StringGrid1.Canvas.FillRect(Rect);
end;
end;

Here's the problem. When the grid rows shift up or down the wrong cell
gets painted. If I'm scrolling down then the red cell travels up to the
top of the control each time I move down one. Conversely if I move up
then the red cell moves down towards the bottom. I thought it might be
a timing issue of the grid getting painted and then being shifted so I
added this code to the TopLeftChanged event:

procedure TForm1.StringGrid1TopLeftChanged(Sender: TObject);
begin
StringGrid1.Repaint;
end;

This seems to fix the problem for the most part BUT what happens is
when scrolling fast through the cells you occasionally see the column
directly above or directly below (depending on which way I'm scrolling)
flicker with red and then jump to row 7.

I know it's minor but there must be a way to prevent this. Any help
would be appreciated.

Thanks,
Jason

.



Relevant Pages

  • Re: Vertical and horizonatal rulers
    ... Actually drawing directly to the Form (or directly into a Picture Box) ... entire game grid into a single large PictureBox ... and therefore which "cell" he is in. ... Scrolling the "larger than the Form" picture Box within the "fully ...
    (microsoft.public.vb.general.discussion)
  • First cell in a datagrid control gets emptied??
    ... But I have a strange problem which I cant seem to fix.. ... in the grid is selected and this causes the cell to be emptied on screen. ...
    (microsoft.public.vb.general.discussion)
  • RE: Finding position of a cell in a datagridview
    ... DataGridView.Scroll event and ajust a set of labels accordingly. ... How can I find the X-Y position of a particular cell (or at least ... Especially if the grid is scrolled horizontally. ... If it weren't for the scrolling, I could at least start at the left ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Finding position of a cell in a datagridview
    ... How can I find the X-Y position of a particular cell (or at least ... Especially if the grid is scrolled horizontally. ... If it weren't for the scrolling, I could at least start at the left ... account for that separately, ...
    (microsoft.public.dotnet.framework.windowsforms)
  • grid.sparse and display
    ... I have a grid with grid.sparce=.f.. ... Unfortunately, the textbox inside the cell, when displayed, was not centered vertically. ... Which properties should I change to fix it? ...
    (microsoft.public.fox.programmer.exchange)