Solved

dbgrid row fonts

Posted on 2002-07-15
6
425 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

830 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