DBGrid: Draw in a cell canvas (SOLVED) lazarus / 31-LCL ---------------------------------------------------------------------- derles · 2017-11-08 15:02 Hello. In a DBGrid I have a Column where I neet to draw a rectangle in each cell. Te rectangle will show a x%, like a horizontal bar graphic. I'm not sure how to draw in a cell canvas. Any ideas ? Tks. Daniel ---------------------------------------------------------------------- wp · 2017-11-08 16:32 The OnDrawCell event is made for any kind of custom painting: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;  aRect: TRect; aState: TGridDrawState);const  xmax = 10;var  x: Integer;  w, h: Integer;begin  if ACol = 2 then begin    if StringGrid1.Cells[ACol-1, ARow] = '' then      exit;    x := StrToInt(StringGrid1.Cells[ACol-1, ARow]);    StringGrid1.Canvas.Brush.Style := bsSolid;    StringGrid1.Canvas.Brush.Color := clBlue;    w := ARect.Right - ARect.Left;    h := (ARect.Bottom - ARect.Top) div 2;    StringGrid1.Canvas.FillRect(      ARect.Left,      (ARect.Top + ARect.Bottom - h) div 2,      ARect.Left + round(x / xmax * w),      (ARect.Top + ARect.Bottom + h) div 2    );  end;end; In this example the first column of the grid contains some integers between 0 and 10. Then this code paints a bar in column 2 according to the value in column. The height of the bar is half a cell height (line 16). Just assign this code to the OnDrawCell event of the grid, no other changes required. [EDIT] Ah, I see after posting that you have a DBGrid, not a StringGrid. Here you get the value to be plotted from the database field in that particular field, but the idea is the same. ---------------------------------------------------------------------- derles · 2017-11-08 18:43 Great WP, you point me in the right direction. Thank you very much. This is my final code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TfrmRanking.dbgClieDrawColumnCell(Sender: TObject;  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);var   x1,y1,x2,y2, iQuart: Integer;begin   if Column.Index = 4 then   begin      if grMax < 1  then  //grMax is the max value of the data column, my 100% width         Exit;       TDBGrid(Sender).Canvas.Brush.Style := bsSolid;      TDBGrid(Sender).Canvas.Brush.Color := clBlue;       iQuart := (Rect.Bottom - Rect.Top) div 4;       x1 := Rect.Left;      y1 := Rect.Top + iQuart;   //the horizontal line bar is centered in the row, from 1/4 to 3/4 height       x2 := Rect.Left + round( (dataRankRPORCENT.AsFloat * Rect.Width) / grMax );      y2 := Rect.Top + (3 * iQuart);       TDBGrid(Sender).Canvas.FillRect(x1,y1,x2,y2);   end;end;