Recent

Author Topic: [ solved ] Dbgrid active cell different color  (Read 2117 times)

John Landmesser

  • New Member
  • *
  • Posts: 25
[ solved ] Dbgrid active cell different color
« on: September 21, 2017, 04:03:49 pm »
how to use  DrawColumnCell to just draw active cell in different color?

must be really simple!!

This code paints the whole active Column but active Cell only?


Code: Pascal  [Select][+][-]
  1.  
  2.   if DBGrid.SelectedField.Index = DataCol
  3.       begin
  4.  
  5.         TDBGrid(Sender).Canvas.Brush.Color := clblue;
  6.         TDBGrid(Sender).Canvas.Font.Style := Font.Style + [fsBold];
  7.         TDBGrid(Sender).Canvas.Font.Color := clRed;
  8.  
  9.       end;
  10.  
  11.  

now searching here:
https://forums.embarcadero.com/thread.jspa?threadID=253161&tstart=0
« Last Edit: September 21, 2017, 07:26:11 pm by John Landmesser »

wp

  • Hero Member
  • *****
  • Posts: 7636
Re: Dbgrid active cell different color
« Reply #1 on: September 21, 2017, 04:17:48 pm »
Don't use OnDrawColumnCell, you'd have to draw everything by yourself. Application of the OnPrepareCell event is much simpler: it is called immediately before the cell is painted, all canvas properties already have been set, but in this event you can override them; a set of parameters comes with the event to decide which cell is painted.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.DBGrid1PrepareCanvas(sender: TObject; DataCol: Integer;
  2.   Column: TColumn; AState: TGridDrawState);
  3. begin
  4.   if ([gdSelected, gdFocused] * AState <> []) and (DBGrid1.SelectedColumn = Column) then
  5.   begin
  6.     DBGrid1.Canvas.Brush.Color := clRed;
  7.     DBGrid1.Canvas.Font.Color := clWhite;
  8.   end;
  9. end;
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

John Landmesser

  • New Member
  • *
  • Posts: 25
Re: Dbgrid active cell different color
« Reply #2 on: September 21, 2017, 07:25:29 pm »
thats's it, works perfect, thanks wp :-)

 

TinyPortal © 2005-2018