Link to home
Start Free TrialLog in
Avatar of coondog091800
coondog091800

asked on

DBGrid Individual Cell Font

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
ASKER CERTIFIED SOLUTION
Avatar of mokule
mokule
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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;
Excuse me, it's my mistake - my comments are about TStringGrid, but question is about TDBGrid ....
Sorry
Avatar of coondog091800
coondog091800

ASKER

Thanks Mokule.  It works great.

Rich