Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

DBGrid Individual Cell Font

Posted on 2005-05-17
5
Medium Priority
?
793 Views
Last Modified: 2013-12-03
Lets say I have a form with a DBgrid, TEdit and a Button.  The DBGrid is linked to the employees table that comes with Delphi through Table1 using DataSource1.  When a user types 'HI' in the TEdit and clicks the button, I want to be able to redraw the DBGrid with every cell that has 'HI' in the "State' column in bold while everything else stays unchanged.  Please give me the code I will need to make this happen.  I know it should be simple but so far my brain is not working today......

Thanks  
Rich
0
Comment
Question by:coondog091800
  • 3
5 Comments
 
LVL 17

Accepted Solution

by:
mokule earned 500 total points
ID: 14022743
This should work for You

procedure TFMain.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  d: TDBGrid;
begin
  if Sender is TDBGrid then
    begin
    d := Sender as TDBGrid;
    d.Canvas.FillRect(Rect);
    if (d.DataSource.DataSet.FieldByName('State').AsString = 'Hi') and (Column.FieldName = 'State') then
      d.Canvas.Font.Style := [fsBold];
    else
      d.Canvas.Font.Style := [];
    d.Canvas.TextOut(Rect.Left,Rect.Top,Column.Field.AsString);
    end;
end;
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 14022812
unit Unit1_Q_21427260;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, Grids;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Edit1: TEdit;
    Button1: TButton;
    Button2: TButton;
    Edit2: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
  private   { Private declarations }
    gValue:   string;
  public    { Public declarations }
  end;

var
  Form1:  TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  StringGrid1.Cells[1,0] := 'State';
  StringGrid1.Cells[1,1] := 'HI';
  StringGrid1.Cells[1,2] := 'Hello';
  StringGrid1.Cells[1,3] := 'HI';
  StringGrid1.Cells[1,4] := 'HI';
  StringGrid1.Cells[1,5] := 'Hello';

  StringGrid1.Cells[2,0] := 'Singer';
  StringGrid1.Cells[2,1] := 'Kylie';
  StringGrid1.Cells[2,2] := 'Minogue';
  StringGrid1.Cells[2,3] := 'Christina';
  StringGrid1.Cells[2,4] := 'Milian';
  StringGrid1.Cells[2,5] := 'Beyonce';

  gValue := 'HI';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  gValue := Edit1.Text;
  StringGrid1.Hide;
  StringGrid1.Show;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  gValue := Edit2.Text;
  StringGrid1.Hide;
  StringGrid1.Show;
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  with StringGrid1 do
  if (Cells[ACol, ARow]=gValue) then
  begin
    Canvas.Font.Style := [fsBold];
    Canvas.TextOut(Rect.Left+2,Rect.Top+2,Cells[ACol, ARow]);
  end;
end;

end.

//........

object Form1: TForm1
  Left = 224
  Top = 128
  BorderIcons = [biSystemMenu, biMinimize]
  BorderStyle = bsSingle
  Caption = 'Form1'
  ClientHeight = 268
  ClientWidth = 562
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  Position = poDefaultPosOnly
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object StringGrid1: TStringGrid
    Left = 20
    Top = 56
    Width = 464
    Height = 192
    ColCount = 3
    RowCount = 6
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goEditing]
    TabOrder = 0
    OnDrawCell = StringGrid1DrawCell
  end
  object Edit1: TEdit
    Left = 20
    Top = 32
    Width = 192
    Height = 21
    TabOrder = 1
    Text = 'HI'
  end
  object Button1: TButton
    Left = 20
    Top = 8
    Width = 192
    Height = 25
    Caption = 'Highlight Text'
    TabOrder = 2
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 296
    Top = 8
    Width = 192
    Height = 25
    Caption = 'Highlight Text'
    TabOrder = 3
    OnClick = Button2Click
  end
  object Edit2: TEdit
    Left = 296
    Top = 32
    Width = 192
    Height = 21
    TabOrder = 4
    Text = 'Hello'
  end
end
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 14024880
Oooops, I did not check about the State Column (in my case Column number 1):

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  with StringGrid1 do
  if (ACol = 1) then
  if (Cells[ACol, ARow]=gValue) then
  begin
    Canvas.Font.Style := [fsBold];
    Canvas.TextOut(Rect.Left+2,Rect.Top+2,Cells[ACol, ARow]);
  end;
end;
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 14024886
Excuse me, it's my mistake - my comments are about TStringGrid, but question is about TDBGrid ....
Sorry
0
 
LVL 1

Author Comment

by:coondog091800
ID: 14026848
Thanks Mokule.  It works great.

Rich
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

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…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

578 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