Call OnDrawCell Directly? lazarus / 13-General ---------------------------------------------------------------------- Madmensking · 2014-08-26 12:25 Afternoon all, I'm trying to work out if it is possible to directly call the onDrawCell procedure to force a String Grid to redraw itself so that cells in a column can be coloured according to if they match values in a string list, the code is below and hopefully will make more sense! Code: [Select]procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer; aRect: TRect; aState: TGridDrawState); var   StringComp: LongInt;   rc: Integer; begin   rc:= ReturnList.Count;   I:= 1;      if rc < 1 then      While i < StringGrid1.RowCount do      begin           if i > rc then exit;           StringComp:=AnsiCompareStr(ReturnList.Strings[i],StringGrid1.Cells[3, i]);           if StringComp = 0 then              begin                StringGrid1.Canvas.Brush.Color:=clGreen;                StringGrid1.Canvas.FillRect(aRect);                StringGrid1.Canvas.TextRect(aRect, aRect.Left, aRect.Top, StringGrid1.Cells[3, i]);                i:= i + 1;                if gdFocused in aState then                   StringGrid1.Canvas.DrawFocusRect(aRect);              end              else              begin                StringGrid1.Canvas.Brush.Color:=clRed;                StringGrid1.Canvas.FillRect(aRect);                StringGrid1.Canvas.TextRect(aRect, aRect.Left, aRect.Top, StringGrid1.Cells[3, i]);                i:= i + 1;                if gdFocused in aState then                   StringGrid1.Canvas.DrawFocusRect(aRect);              end;      end; end;                                        NOTE The variable i is declared as a public variable and is used a generic counter for loops. ---------------------------------------------------------------------- Mike.Cornflake · 2014-08-26 12:36 Don't know about OnDrawCell, but with TDBGrid, I use OnPrepareCanvas for similar... http://lazarus- ccr.sourceforge.net/docs/lcl/grids/tcustomgrid.onpreparecanvas.html Here's my code for TDBGrid.  I note the parameters for PrepareCanvas are different for TStringGrid (for TStringGrid you get Row and Col passed as parameters), but you should get the idea...  Basically, I'm only setting Canvas.Brush.Colour and Canvas.Font.Colour, the grid does the actual drawing itself. Code: [Select]Procedure TFrameGrid.grdSQLPrepareCanvas(Sender: TObject; DataCol: Integer;   Column: TColumn; AState: TGridDrawState); Var   oField: TField;   oColor: TColor;   sColor: String; Begin   If Not (gdSelected In AState) Then   Begin     oField := FDataset.FindField('Colour_ID');     If Not Assigned(oField) Then       oField := FDataset.FindField('Color_ID');     If Assigned(oField) Then     Begin       sColor := Lowercase(oField.Text);       Case sColor Of         'red': oColor := TColor($008080FF);         'green': oColor := TColor($0080FF80);         'blue': oColor := TColor($00FF9F40);         'orange': oColor := TColor($0084ACFF);         'yellow': oColor := TColor($0080FFFF);         Else           sColor := 'ignore';       End;       If sColor <> 'ignore' Then       Begin         If gdRowHighlight In AState Then           oColor := GetHighLightColor(oColor, 32);         grdSQL.Canvas.Brush.Color := oColor;       End;       grdSQL.Canvas.Font.Color := clBlack;     End;   End; End; ---------------------------------------------------------------------- Madmensking · 2014-08-26 12:48 Cheers Mike, I will give that a go, I notice it doesn't use any TRect though so does the code you've given colour the entire row rather than an individual cell? ---------------------------------------------------------------------- Blaazen · 2014-08-26 12:51 I guess you cannot call StringGrid1DrawCell directly, or, better said, you can but is no guarantee that anything will be painted. Some wigdetsets, like Qt and Carbon, does not allow to paint out of Paint method. Maybe it will work on Windows and GTK2. A more straightforward method is to assign the OnDrawCell event to the grid and call StringGrid1.Invalidate. Job will be done in a natural way. ---------------------------------------------------------------------- Madmensking · 2014-08-26 12:54 Blaazen, I tried using .invalidate but nothing happened? Even when I put a break in at the beginning on the drawcell after the invalidate had been run, ondrawcell was not called.. ---------------------------------------------------------------------- Mike.Cornflake · 2014-08-26 12:58 Quote from: Madmensking on August 26, 2014, 02:48:55 pmCheers Mike, I will give that a go, I notice it doesn't use any TRect though so does the code you've given colour the entire row rather than an individual cell? It's the different parameters thing I was talking about.  For TStringGrid, your parameters will include Row and Column, so you'll be doing for an individual cell. My code is actually suboptimal as I'm doing the same Field Lookup once for every column per record in the dataset.  And I've only just noticed, so thanks for your question  As for the lack of rect, there's no drawing going on inside PrepareCanvas.  The grid does the drawing.  It just uses the brush, pen, font etc that you set up for it in OnPrepareCanvas.