Delphi StringGrid refresh

483 Views Asked by At

Learning Delphi (have a ways to go), using Rio.

I figured out how to use a colored background in TStringGrid rows - but it looks like I need to refresh when data in those rows changes (so as to get different colors to show up based on the data changes).

I thought that just setting the cell values to their then-existing values would cause the refresh. But it didn't. I could tell for sure that it didn't - because I had a debug breakpoint placed within the StringGrid1DrawCell procedure - and that breakpoint was not hit.

The code that I had been using to hopefully cause the refresh in TStringGrid was as follows (note: S is defined as a String):

S := StringGrid1.Cells[1, i];
StringGrid1.Cells[1, i] := S;

Is the basic assumption (that just setting/resetting values of the cell contents causes a refresh) in error?

If the idea is right, but the method is wrong: could you let me know what to do differently?

1

There are 1 best solutions below

4
Remy Lebeau On

The OnDrawCell event is fired only when a given cell needs to be painted onscreen.

Setting a specific cell's value will invalidate only that cell, thus triggering a repaint of only that cell, not the cell's entire row, or the grid as a whole.

If you need to trigger a repaint of an entire row, call the grid's (protected) InvalidateRow() method, eg:

type
  TStringGridAccess = class(TStringGrid)
  end;

procedure TMyForm.DoSomething;
begin
  ... 
  StringGrid1.Cells[1, i] := ...; 
  TStringGridAccess(StringGrid1).InvalidateRow(i);
  ...
end;

If you need to trigger a repaint of the entire grid, call the grid's (public) Invalidate() method, eg:

StringGrid1.Cells[1, i] := ...;
StringGrid1.Invalidate;