• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 612
  • Last Modified:

The brush color in DBGrid

Dear Experts, please help !

   When I entered the edit mode in dbgrid, I wish the background color of the editor in dbgrid changed into another color ( clAqua ).
And this is what I've done, but it doesn't work ?

Thanks from apin

p.s : I intend not to use the OnDrawDataCell or OnDrawColoumnCell procedure.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Db, Grids, DBGrids, DBTables, StdCtrls;

type
  TForm1 = class(TForm)
    Table1: TTable;
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    Label1: TLabel;
    procedure FormActivate(Sender: TObject);
    procedure DBGrid1KeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

 type
   MyDBGrid = class(TDBGrid)
   protected
     function CreateEditor: TInplaceEdit; override;
  end;

 type
   The_InplaceEdit = class(TInplaceEdit)
   public
     property Color ;
 End ;

var
  Form1   : TForm1;

implementation

{$R *.DFM}


function MyDBGrid.CreateEditor: TInplaceEdit;
begin
   Result := inherited CreateEditor;
   The_InplaceEdit(Result).Brush.Color:= clAqua ;
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
  With MyDBGrid(DBGrid1) Do Begin
     Font.Color := clGreen ;
     Font.Size := 14 ;
  End ;
end;

procedure TForm1.DBGrid1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  B: byte;
begin
  With MyDBGrid(DBGrid1) Do begin
     for B := 0 to ControlCount - 1 do begin
         with Controls[B] as TInPlaceEdit do begin
           Label1.Caption := 'Text = ' + Text;
         end; // With
     end ; // for
  End ;
end;


end.
0
pc_melsa
Asked:
pc_melsa
  • 4
  • 3
1 Solution
 
TOndrejCommented:
You have declared a new class MyDBGrid but on your form you put a TDBGrid.
Therefore the grid you see at runtime is an instance of TDBGrid, not MyDBGrid, and typecasting with MyDBGrid(DBGrid1) has no effect; your overriden MyDBGrid.CreateEditor is never called.

What you need to do instead is create an instance of MyDBGrid. Either install MyDBGrid into the component palette and put in on your form just like with TDBGrid, or create it in code, e.g. in the form's OnCreate event:

type
  TForm1 = class(TForm)
  ...
  private
    FMyDBGrid: MyDBGrid;
  end;

implementation

procedure TForm1.FormCreate(Sender: TObject);
begin
  with MyDBGrid.Create(Self) do
    DataSource := DataSource1;
end;

In your overriden MyDBGrid.CreateEditor it should be enough to do

function MyDBGrid.CreateEditor: TInplaceEdit;
begin
  Result := inherited CreateEditor;
  The_InplaceEdit(Result).Color:= clAqua ;
end;

HTH
0
 
TOndrejCommented:
sorry the declaration of private FMyDBGrid: MyDBGrid in the form is not necessary, I forgot to delete it.

I hope it's not confusing, I'll send another comment with the intended text.
0
 
TOndrejCommented:
Another try, here's what I intended to post:

You have declared a new class MyDBGrid but on your form you put a TDBGrid.
Therefore the grid you see at runtime is an instance of TDBGrid, not MyDBGrid, and typecasting with
MyDBGrid(DBGrid1) has no effect; your overriden MyDBGrid.CreateEditor is never called.

What you need to do instead is create an instance of MyDBGrid. Either install MyDBGrid into the component
palette and put in on your form just like with TDBGrid, or create it in code, e.g. in the form's OnCreate
event:

procedure TForm1.FormCreate(Sender: TObject);
begin
 with MyDBGrid.Create(Self) do
   DataSource := DataSource1;
end;

In your overriden MyDBGrid.CreateEditor it should be enough to do

function MyDBGrid.CreateEditor: TInplaceEdit;
begin
 Result := inherited CreateEditor;
 The_InplaceEdit(Result).Color:= clAqua ;
end;

HTH
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
pc_melsaAuthor Commented:
Dear Mr. HTH,
Thanks for your explanation, it works ! Below is the component I?ve created. But another problem arise. When I want to change the color ( from clAqua to clRed, for instance ), I must change the unit file of the component and recompile again.
Please help me again.

     
Best Regards,
Apin


unit My_DBGrid;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, DBGrids;

type
  MyDBGrid = class(TDBGrid)

  published
    { Published declarations }
  end;

type
  The_InplaceEdit = class(TInplaceEdit)
  public
    property Color ;
End ;

procedure Register;

implementation

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

function MyDBGrid.CreateEditor: TInplaceEdit;
begin
  Result := inherited CreateEditor;
  The_InplaceEdit(Result).Brush.Color:= clYellow ;
end;
end.

0
 
TOndrejCommented:
Simply declare a published property InplaceEditColor:

type
  MyDBGrid = class(TDBGrid)
  private
    FInplaceEditColor: TColor;
  protected
    function CreateEditor: TInplaceEdit;
  public
    constructor Create(AOwner: TComponent); override;
  published
    property InplaceEditColor: TColor read FInplaceEditColor write FInplaceEditColor default clAqua;
  end;

implementation

function MyDBGrid.CreateEditor: TInplaceEdit;
begin
  Result := inherited CreateEditor;
  The_InplaceEdit(Result).Color := FInplaceEditColor;
end;

constructor MyDBGrid.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FInplaceEditColor := clAqua;
end;

Properties declared in the published section are stored with the component into the .dfm stream.
The constructor is really needed only to initialize the property to the default value.
When the component is being saved into the .dfm stream and the property has the value that was declared default then the property is not saved into the .dfm - this saves some space.

HTH = Hope This Helps :-)

TOndrej

P.S. Why grade B?
0
 
pc_melsaAuthor Commented:
Dear Mr. TOndrey,
    Thank you very much, this is actually a great help for me !
May God Bless You !

Best Regards,
apin
0
 
pc_melsaAuthor Commented:
Dear Mr. TOndrey,
     Although the InplaceEditColor property appears in the object inspector, but the back ground color of the grid did not change to clAqua ( or any color I've selected ).

Please tell me why.

Thanks from apin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now