Recent

Author Topic: Coloring rows in a dbgrid.  (Read 360 times)

melwinek

  • Jr. Member
  • **
  • Posts: 60
Coloring rows in a dbgrid.
« on: May 17, 2020, 09:03:31 pm »
I color rows with e.g.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.RxDBGrid1PrepareCanvas(sender: TObject;
  2.   DataCol: Integer; Column: TColumn; AState: TGridDrawState);
  3. begin
  4.       if SQLQuery1['s']<>null then RxDBGrid1.Canvas.Brush.Color:=$0099FF99;
  5. end;
         

But how to make coloring of rows, based on the content of the cell of the selected row.
e.g.
Selected row has an "id" cell, and has an empty "masterid" cell. This row is not to be colored.
I want to color the rows in which masterid=id (id not from this row only from the one I described above.


Bart

  • Hero Member
  • *****
  • Posts: 3857
    • Bart en Mariska's Webstek
Re: Coloring rows in a dbgrid.
« Reply #1 on: May 17, 2020, 09:18:32 pm »
Use OnPrepareCancas event?
At leats that's how it works in a TStringGrid.

Bart

zoltanleo

  • Sr. Member
  • ****
  • Posts: 257
Re: Coloring rows in a dbgrid.
« Reply #2 on: May 18, 2020, 01:31:17 am »
Hi melwinek. Try this code
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   MemDataset1.Active:= True;
  4.   MemDataset1.AppendRecord([1,12,'qqq']);
  5.   MemDataset1.AppendRecord([2,null,'www']);
  6.   MemDataset1.AppendRecord([3,3,'eee']);
  7.   MemDataset1.AppendRecord([4,null,'rrr']);
  8.   MemDataset1.AppendRecord([5,5,'ttt']);
  9.   MemDataset1.AppendRecord([5,7,'yyy']);
  10.  
  11.   with DBGrid1 do
  12.   begin
  13.     Options:= Options + [dgRowHighlight];
  14.     Options:= Options + [dgRowSelect];
  15.   end;
  16. end;
  17.  
  18. procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  19.   DataCol: Integer; Column: TColumn; State: TGridDrawState);
  20. begin
  21.   if gdSelected in State then
  22.     begin
  23.       if (Column.Field.DataSet.FieldByName('id').Value) =
  24.                     (Column.Field.DataSet.FieldByName('master_id').Value) then
  25.       begin
  26.         TDBGrid(Sender).Canvas.Brush.Color:= clYellow;
  27.         TDBGrid(Sender).Canvas.Font.Color:= clRed;
  28.         TDBGrid(Sender).Canvas.FillRect(Rect);
  29.       end;
  30.  
  31.       if Column.Field.DataSet.FieldByName('master_id').IsNull then
  32.       begin
  33.         TDBGrid(Sender).Canvas.Brush.Color:= clDefault;
  34.         TDBGrid(Sender).Canvas.Font.Color:= clDefault;
  35.         TDBGrid(Sender).Canvas.FillRect(Rect);
  36.         if DataCol = 1 then //this is optional
  37.           TDBGrid(Sender).Canvas.TextOut(Rect.Left+2,Rect.Top+2,'Empty value');
  38.       end;
  39.     end;
  40.  
  41.   TDBGrid(Sender).DefaultDrawColumnCell(Rect,DataCol,Column,State);
  42. end;    
   

                           
« Last Edit: May 18, 2020, 01:43:40 am by zoltanleo »
Win10 LTSC x64/Deb 10 amd64:
Lazarus x32/x64 2.1(r.63554); FPC 3.3.1 (r.45871), FireBird 3.0.6

Sorry for my bad English, I'm using translator ;)

 

TinyPortal © 2005-2018