Solved

dbgrid row fonts

Posted on 2002-07-15
6
423 Views
Last Modified: 2013-12-02
I have a dbgrid on a form and I need to change the font colour of individual rows depending on a value in that row. Is there any way of doing this?
0
Comment
Question by:riccohb
  • 2
  • 2
  • 2
6 Comments
 
LVL 2

Expert Comment

by:mullet_attack
ID: 7154667
yes, Write an OnDrawColumnCell event handler to provide customized drawing for the data in the cells of the grid. Draw on the cell using the methods of the Canvas property.

set the grid default drawing to false
0
 
LVL 2

Expert Comment

by:mullet_attack
ID: 7154686
here's a sample code. it draw buttons on a single column grid so it looks like just a column of buttons. I used OnDrawDataCell  even tho it's supposed to be obsolete...

the button bitmaps are held in image1 and image2, not exactly what you want but it should give you the idea...

if the text has a '^' in it, it makes smaller font and writes it on two lines. It was for an app where the form 'look and feel' was dependant on database table contents.

Anyway, you get the idea...

procedure TfrmOrder.grdGroupsDrawDataCell(Sender: TObject; const Rect: TRect;
  Field: TField; State: TGridDrawState);
var
  Text, TextA, TextB : string;
  fSize : integer;
  TopOffset1, TopOffset2 : integer;
  LeftOffset : integer;
begin
  with (Sender as TwwDBGrid).canvas do
    begin
      if field = nil then exit;
      if Field.AsString = '' then
        begin
          Brush.Color := clBlack;
          FillRect(rect);
          exit;
        end;
      Text := Field.AsString;
      if Pos('^',Text) > 0 then
        begin
          TextA := Copy(Text,1,Pos('^',Text)-1);
          TextB := Copy(Text,Pos('^',Text) + 1, Length(Text));
          fSize := 12;
          TopOffset1 := 5;
          TopOffset2 := 11;
        end
      else
        begin
          TextA := Text;
          TextB := '';
          fSize := 18;
          TopOffset1 := 10;
          TopOffset2 := 20;
        end;
      if gdSelected in State then
          StretchBlt(Handle,
                     Rect.left,
                     Rect.top,
                     Rect.Right - Rect.Left,
                     Rect.Bottom - Rect.Top,
                     image1.canvas.handle,
                     image1.ClientRect.left,
                     image1.ClientRect.top,
                     image1.ClientRect.Right - image1.ClientRect.Left,
                     image1.ClientRect.Bottom - image1.ClientRect.Top,
                     SrcCopy)
      else
          StretchBlt(Handle,
                     Rect.left,
                     Rect.top,
                     Rect.Right - Rect.Left,
                     Rect.Bottom - Rect.Top,
                     image2.canvas.handle,
                     image2.ClientRect.left,
                     image2.ClientRect.top,
                     image2.ClientRect.Right - image2.ClientRect.Left,
                     image2.ClientRect.Bottom - image2.ClientRect.Top,
                     SrcCopy);
          font.color := clWhite;
          font.size := fSize;
          LeftOffset := (Rect.Right - Rect.Left - TextWidth(TextA)) div 2;
          TextOut(Rect.left + LeftOffset, Rect.top + TopOffset1, TextA);
          LeftOffset := (Rect.Right - Rect.Left - TextWidth(TextB)) div 2;
          TextOut(Rect.left + LeftOffset, Rect.top + TopOffset2 + fSize, TextB);
     end;
end;
0
 

Author Comment

by:riccohb
ID: 7154820
Another slight problem. I've added an OnDrawDataCell event, but it isn't being called. I've added a breakpoint on the first line of the procedure but it never stops there. (I've set DefaultDrawing to False).

Any ideas?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Accepted Solution

by:
plaster earned 200 total points
ID: 7156914
May or may not but this works for me :)

procedure TDelivery.DBGrid2DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
var OldBrush,
    OldFont: TColor;
      Index: Integer;
begin
  with (Sender as TDBGrid), Canvas do begin
    OldBrush:=Brush.Color;
    OldFont:=Font.Color;

   try
    if {(Sender as TDBGrid).DataSource.DataSet.FieldByName('qtydel').AsString}= (Sender as TDBGrid).DataSource.DataSet.FieldByName('qty').AsString then begin
      if gdSelected in State then begin
        Brush.Color:=clNavy;
        Font.Color:=clwhite;
      end
      else begin
        Brush.Color:=clyellow;
        Font.Color:=clred;
      end
    end;
   except
     Brush.Color:=OldBrush;
     Font.Color:=OldFont;
   end;
   DefaultDrawColumnCell(Rect, DataCol, Column, State);
   Brush.Color:=OldBrush;
   Font.Color:=OldFont;
 end;
0
 
LVL 1

Expert Comment

by:plaster
ID: 7156918
remove {  }  from if statement
0
 

Author Comment

by:riccohb
ID: 7156987
Like a charm :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi component that can load a DLL in design time? 8 53
Show Listview image from database (String field) 5 114
Delphi 2 51
Graphics32 under Delphi 10.1 Berlin 2 40
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now