Show a ROW of a DBGrid in a different color  

Send By: Radikal (Q3 Team)
Web : http://www.q3.nu
Email: radikal@q3.nu
Date: 04/02/01

Tip accessed 171 times

 


We have another trick to show columns of a DBGrid in different color, in short, the one:

[61] - Display a DBGrid column in a different color

In this other one, we will change the color of a whole line, that is to say, of a whole register (a register or those that complete a certain condition).

The theme is to get that the comparison of if it is a record to show of another color, be not carried out once for each field of each record, but once for registration, winning this way in speed.
For it, we will calculate the color with which will paint the record just in one of the columns of the DBGrid, using this color calculated in the rest of the columns.
So that this serves?, for example, do imagine a list of clients shown in a DBGrid... could we mark in red those clients that owe us money...

The example:

We will use the Animals.DBF table of the Delphi's demos:

  • Put a Table and asociate it with Animals.DBF table of the Delphi's demos
  • Put a Datasource link to the table
  • Put a DBGrid (DBGrid1) link to DataSource
  • Declare a TColor variable in the private of the form:

       private
         { Private declarations }
         UnColor:TColor;
    



  • Put this code in the OnDrawColumnCell of the DBGrid1:


     procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
       DataCol: Integer; Column: TColumn; State: TGridDrawState);
     begin
       {Si es la primera columna, calculamos el color}
       {UnColor debe estar declarada global a la form}
    
       {If it is the first column, we calculate the color}
       {UnColor should be declared as global to the form}
       if DataCol=0 then
         if Table1Size.AsInteger>10
           then UnColor:=clRed
           else UnColor:=clBlack;
    
       with (Sender As TDBGrid).Canvas do
       begin
         Font.Color:=UnColor;
         FillRect(Rect);
         TextOut(Rect.Left, Rect.Top, Column.Field.AsString);
       end;
     end;
    




    A tip improvement:


     procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
       DataCol: Integer; Column: TColumn; State: TGridDrawState);
     begin
       {Si es la primera columna, calculamos el color}
       {UnColor debe estar declarada global a la form}
    
       {If it is the first column, we calculate the color}
       {UnColor should be declared as global to the form}
       if DataCol=TStringGrid(Sender).LeftCol-1 then
         if Table1Size.AsInteger>10
           then UnColor:=clRed
           else UnColor:=clBlack;
    
       with (Sender As TDBGrid).Canvas do
       begin
         Font.Color:=UnColor;
         FillRect(Rect);
         TextOut(Rect.Left, Rect.Top, Column.Field.AsString);
       end;
     end;
    




    Sent by: Santy Concepción (SANTYCG@terra.es)



     procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
       DataCol: Integer; Column: TColumn; State: TGridDrawState);
     const
       clPaleGreen = TColor($CCFFCC);
       clPaleRed =   TColor($CCCCFF);
     begin
         If Column.Field.Dataset.FieldbyName('CANTIDAD').AsInteger < 5
           then
            If (gdFocused in State)
             then dbgrid1.canvas.brush.color := clBlack
             else dbgrid1.canvas.brush.color := clPaleGreen;
    
         //Esta línea es nueva
    
         dbgrid1.DefaultDrawColumnCell(rect,DataCol,Column,State)
     end;
    





    Updated at 11/03/2000


    Updated at 04/02/2001 (Santy improvement)