John,

you can just display an image of a checkbox in the DrawColumnCell event -
see below code (written by someone else)

cheers,
Steve

Displaying a checkbox within a DBGrid for use with Boolean fields
Displaying a checkbox in a DB Grid doesn't necesarily mean you have to have
a TCheckBox control on the form.  By owner drawing the grid control the same
effect can be achieved. 

The following component overrides the default behaviour of TDBGrid this
allows us to draw a checkbox within the cell.  Because some databases don't
support the Boolean datatype an extra property has been added.  The
BoolFields property lists all column's which are boolean. 
 


unit DBGridEx; 
{DBGrid which displays a checkbox instead of the default editor for 
all boolean fields. 

Created by Simon Carter ([EMAIL PROTECTED]) 
} 

interface 

uses 
  Windows, Classes, Grids, DBGrids; 

type 
  TDBGridEx = class(TDBGrid) 
  private 
    FBoolFields: TStringList; 
    procedure SetBoolFields(const Value: TStringList); 
  protected 
    procedure DrawColumnCell(const Rect: TRect; DataCol: Integer; 
      Column: TColumn; State: TGridDrawState); override; 
    procedure CellClick(Column: TColumn); override; 
    procedure ColEnter; override; 
    procedure ColExit; override; 
  public 
    constructor Create(AOwner: TComponent); override; 
    destructor Destroy; override; 
  published 
    property BoolFields: TStringList read FBoolFields write SetBoolFields; 
  end; 

procedure Register; 

implementation 

uses DB; 

procedure Register; 
begin 
  RegisterComponents('Samples', [TDBGridEx]); 
end; 

constructor TDBGridEx.Create(AOwner: TComponent); 
begin 
  inherited Create(AOwner); 
  FBoolFields := TStringList.Create; 
end; 

destructor TDBGridEx.Destroy; 
begin 
  FBoolFields.Free; 
  inherited Destroy; 
end; 

procedure TDBGridEx.DrawColumnCell(const Rect: TRect; DataCol: Integer; 
  Column: TColumn; State: TGridDrawState); 
begin 
  inherited DrawColumnCell(Rect, DataCol, Column, State); 
  if (Column.Field.DataType = ftBoolean) or 
    (FBoolFields.IndexOf(Column.Field.FieldName) > -1) then 
  begin 
    with Canvas do 
    begin 
      FillRect(Rect); 
      if Column.Field.AsBoolean then 
        DrawFrameControl(Handle, Rect, DFC_BUTTON, 
          DFCS_BUTTONCHECK or DFCS_CHECKED) 
      else 
        DrawFrameControl(Handle, Rect, DFC_BUTTON, DFCS_BUTTONCHECK); 
    end; 
  end; 
end; 

procedure TDBGridEx.CellClick(Column: TColumn); 
begin 
  if (Column.Field.DataType = ftBoolean) or 
    (FBoolFields.IndexOf(Column.Field.FieldName) > -1) then 
  begin 
    if not (DataSource.DataSet.State in [dsEdit]) then 
      DataSource.DataSet.Edit; 
    Column.Field.AsBoolean := not Column.Field.AsBoolean; 
  end; 
  inherited CellClick(Column); 
end; 

procedure TDBGridEx.ColEnter; 
begin 
  if (SelectedField.DataType = ftBoolean) or 
    (FBoolFields.IndexOf(SelectedField.FieldName) > -1) then 
  begin 
    InplaceEditor.Hide; 
    Options := Options - [dgEditing, dgAlwaysShowEditor]; 
  end; 
  inherited ColEnter; 
end; 

procedure TDBGridEx.ColExit; 
begin 
  inherited ColExit; 
  Options := Options + [dgEditing, dgAlwaysShowEditor]; 
end; 

procedure TDBGridEx.SetBoolFields(const Value: TStringList); 
begin 
  FBoolFields.Assign(Value); 
end; 

end. 

> -----Original Message-----
> From: John [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, 5 August 2003 8:33 a.m.
> To: Multiple recipients of list delphi
> Subject: [DUG]: TDBGrid - tick
> 
> 
> Hi all,
> 
> How can I get showing a TICK in a TDBGrid's logical field. I 
> know how to
> change text from "true" to "Yes", by setting the 
> DisplayValues property on
> the associated table component. But how can I change this to the TICK
> character (character 251)?
> Or should I load an image? If so, how do I do that?
> 
> TIA
> 
> John.
> 
> --------------------------------------------------------------
> -------------
>     New Zealand Delphi Users group - Delphi List - 
> [EMAIL PROTECTED]
>                   Website: http://www.delphi.org.nz
> To UnSub, send email to: [EMAIL PROTECTED] 
> with body of "unsubscribe delphi"
> Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/
> 
> ---
> Incoming mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.507 / Virus Database: 304 - Release Date: 4/08/2003
>  
> 
---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED] 
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/

Reply via email to