Recent

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

John Landmesser

  • New Member
  • *
  • Posts: 31
[ 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: 11858
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;

John Landmesser

  • New Member
  • *
  • Posts: 31
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