Recent

Author Topic: DBGrid: Draw in a cell canvas (SOLVED)  (Read 5394 times)

derles

  • New Member
  • *
  • Posts: 35
DBGrid: Draw in a cell canvas (SOLVED)
« on: November 08, 2017, 04:02:56 pm »
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
« Last Edit: November 08, 2017, 07:44:07 pm by derles »

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: DBGrid: Draw in a cell canvas
« Reply #1 on: November 08, 2017, 05:32:28 pm »
The OnDrawCell event is made for any kind of custom painting:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
  2.   aRect: TRect; aState: TGridDrawState);
  3. const
  4.   xmax = 10;
  5. var
  6.   x: Integer;
  7.   w, h: Integer;
  8. begin
  9.   if ACol = 2 then begin
  10.     if StringGrid1.Cells[ACol-1, ARow] = '' then
  11.       exit;
  12.     x := StrToInt(StringGrid1.Cells[ACol-1, ARow]);
  13.     StringGrid1.Canvas.Brush.Style := bsSolid;
  14.     StringGrid1.Canvas.Brush.Color := clBlue;
  15.     w := ARect.Right - ARect.Left;
  16.     h := (ARect.Bottom - ARect.Top) div 2;
  17.     StringGrid1.Canvas.FillRect(
  18.       ARect.Left,
  19.       (ARect.Top + ARect.Bottom - h) div 2,
  20.       ARect.Left + round(x / xmax * w),
  21.       (ARect.Top + ARect.Bottom + h) div 2
  22.     );
  23.   end;
  24. 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.
« Last Edit: November 08, 2017, 05:34:19 pm by wp »

derles

  • New Member
  • *
  • Posts: 35
Re: DBGrid: Draw in a cell canvas (SOLVED)
« Reply #2 on: November 08, 2017, 07:43:29 pm »
Great WP, you point me in the right direction. Thank you very much.
This is my final code:

Code: Pascal  [Select][+][-]
  1. procedure TfrmRanking.dbgClieDrawColumnCell(Sender: TObject;
  2.   const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
  3. var
  4.    x1,y1,x2,y2, iQuart: Integer;
  5. begin
  6.    if Column.Index = 4 then
  7.    begin
  8.       if grMax < 1  then  //grMax is the max value of the data column, my 100% width
  9.          Exit;
  10.  
  11.       TDBGrid(Sender).Canvas.Brush.Style := bsSolid;
  12.       TDBGrid(Sender).Canvas.Brush.Color := clBlue;
  13.  
  14.       iQuart := (Rect.Bottom - Rect.Top) div 4;
  15.  
  16.       x1 := Rect.Left;
  17.       y1 := Rect.Top + iQuart;   //the horizontal line bar is centered in the row, from 1/4 to 3/4 height
  18.  
  19.       x2 := Rect.Left + round( (dataRankRPORCENT.AsFloat * Rect.Width) / grMax );
  20.       y2 := Rect.Top + (3 * iQuart);
  21.  
  22.       TDBGrid(Sender).Canvas.FillRect(x1,y1,x2,y2);
  23.    end;
  24. end;
  25.  

 

TinyPortal © 2005-2018