Recent

Author Topic: [SOLVED] DBGrid icon  (Read 1602 times)

Pe3s

  • Sr. Member
  • ****
  • Posts: 483
[SOLVED] DBGrid icon
« on: June 05, 2023, 08:51:20 pm »
Hello, how can I put an icon in the DBGrid component in any column?
« Last Edit: June 11, 2023, 09:19:19 am by Pe3s »

dsiders

  • Hero Member
  • *****
  • Posts: 915
Re: DBGrid icon
« Reply #1 on: June 05, 2023, 09:04:53 pm »
Hello, how can I put an icon in the DBGrid component in any column?

You can use the OnDrawColumnCell event handler (TDrawColumnCellEvent) if it's not a fixed cell.
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

Pe3s

  • Sr. Member
  • ****
  • Posts: 483
Re: DBGrid icon
« Reply #2 on: June 06, 2023, 06:26:05 pm »
Thank you :)

Pe3s

  • Sr. Member
  • ****
  • Posts: 483
Re: DBGrid icon
« Reply #3 on: June 10, 2023, 11:52:44 am »
Hello, how can I improve the code so that it does not draw bold text in columns?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  2.   DataCol: Integer; Column: TColumn; State: TGridDrawState);
  3. begin
  4.    if Column.FieldName = 'Category' then begin
  5.       ImageList1.Draw(DBGrid1.Canvas, Rect.Left, Rect.Top, 0)
  6.   end;
  7.     DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
  8. end;
  9.  

wp

  • Hero Member
  • *****
  • Posts: 11209
Re: DBGrid icon
« Reply #4 on: June 10, 2023, 01:41:50 pm »
Hello, how can I improve the code so that it does not draw bold text in columns?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  2.   DataCol: Integer; Column: TColumn; State: TGridDrawState);
  3. begin
  4.    if Column.FieldName = 'Category' then begin
  5.       ImageList1.Draw(DBGrid1.Canvas, Rect.Left, Rect.Top, 0)
  6.   end;
  7.     DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
  8. end;
  9.  
"does not draw bold text in columns" - if this means: the text in the columns should not be bold, the answer is: Don't add the fsBold style to the DBGrid1.Canvas.Font.Style or DBGrid1.Font.Style - you must be doing this somewhere because this is not default behaviour, but, of course, I don't know where you do this since you do not show this code.

If it means: there should not be ANY text in the columns, the answer is: Remove the call to DBGrid1.DefaultDrawColumnCell from above code.

Pe3s

  • Sr. Member
  • ****
  • Posts: 483
Re: DBGrid icon
« Reply #5 on: June 10, 2023, 02:00:30 pm »
@wp, I never use bold text.
After writing the procedure that draws the icon, it looks like it's superimposing text on top of the text, so it appears thicker

paweld

  • Hero Member
  • *****
  • Posts: 785
Re: DBGrid icon
« Reply #6 on: June 10, 2023, 02:13:11 pm »
Two options: don't add a text drawing call or clear the cell at the beginning of OnDrawCell: https://lazarus-ccr.sourceforge.io/docs/lcl/graphics/tcanvas.fillrect.html
Best regards / Pozdrawiam
paweld

Pe3s

  • Sr. Member
  • ****
  • Posts: 483
Re: DBGrid icon
« Reply #7 on: June 11, 2023, 09:19:02 am »
Thank you

Pe3s

  • Sr. Member
  • ****
  • Posts: 483
Re: [SOLVED] DBGrid icon
« Reply #8 on: June 11, 2023, 09:46:50 am »
My solution:
DBgrid -> DefaultDraw: False;

Code: Pascal  [Select][+][-]
  1. procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  2.   DataCol: Integer; Column: TColumn; State: TGridDrawState);
  3. var
  4.   LRating: Extended;
  5. begin
  6.    if Column.FieldName.Equals('Rating') then
  7.    begin
  8.      LRating := Column.Field.AsInteger;
  9.      ImageList1.Draw(DBGrid1.Canvas, Rect.CenterPoint.X - (ImageList1.Width div 2), Rect.CenterPoint.Y - (ImageList1.Height div 2), Trunc(LRating));
  10.   end else
  11.     DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
  12. end;
  13.  

 

TinyPortal © 2005-2018