Solved

dbgrid row fonts

Posted on 2002-07-15
6
426 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
oracle global variables 4 80
Convert GUI app into console app for Win32 Env 5 133
how can i search if string exist in array ? 3 77
Delphi: sending SMS on android platform 1 98
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

737 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