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

Custom DBGrid Component

hello,

    i want to create a component inherited from DBGrid. what i want to do in this component is to have a picture displayed on a certain column aside from the fields column queried from the database.

    below is the code i tried using on my application and is working. since i will be using this throughout my application i wish to convert it to a component. i have tried converting but i was stucked on the image to be added. i want to have an additional property (Picture) where i can define the location of the picture i want and use this on all the rows displayed.

TIA ;-)

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  bmp : TBitmap;  
begin
  if Column.FieldName = '' then
    with Canvas do
    begin
      Brush.Bitmap := Image1.Picture.Bitmap;
      StretchDraw(Rect, Image1.Picture.Bitmap);
    end;
end;
0
third
Asked:
third
1 Solution
 
thirdAuthor Commented:
"with Canvas do"

im sorry that should be

"with DBGrid1.Canvas do"
0
 
DragonSlayerCommented:
I think you can just replace it with

with DBGrid1.Canvas do
  CopyRect(Rect, Image1.Canvas, Image1.Canvas.ClipRect);
0
 
thirdAuthor Commented:
no, what i would like to have is the component code on how you will transform that function into a Custom DBGrid Component which has a "Picture" property added to let the user select a picture when the component is used.

the picture property i'm talking about is similar to the picture property of TImage where a picture editor pops-up to let the user load whatever image he may want. so it's like an ordinary DBGRid component with an added picture property.

anybody seen the User Manager for WinNT? it will look closely similar to that.

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
swift99Commented:
Check out Developers Express tools.  They have a lot of this stuff already built.  If this is a commercial project, weigh the $1K or so against your time to build the equivalent tools.

If this is a self-enlightnement project, add a picture property and override the paint logic of the grid to support your image functions.  The worst case scenario is that if you need to override something that is hidden then you clone the Grids and DBGrids modules, rename the classes so that they don't interfere with the VCL, make the properties and methods you need protected and/or virtual, and descend your component from those.  I had to do this once for an oddball control I needed and it was much easier than I expected.
0
 
alx512Commented:
You must inherite new component from TDBGrid.

TMyDBGrid = class(TDBGrid)
private
  FPicture: TPicture;
  procedure SetPicture(Value: TPicture);
published
  property Picture: TPicture read FPicture write SetPicture;  
public
  constructor Create(AOwner: TComponent);
  destructor Destroy; override;
end;

procedure TMyDBGrid.SetPicture(Value: TPicture);
begin
  FPicture.Assign(Value);
end;

constructor TMyDBGrid.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FPicture := TPicture.Create;
end;

destructor TMyDBGrid.Destroy;
begin
  FPicture.Free;
  inherited;
end;

// Then following code that draw picture.
// You can override the DrawCell procedure or assign the
// procedure to OnDrawCell event.

0
 
bes67Commented:
Here a post... and notice an Indent property (if I right understand what you need)

unit PicDBGrid;

interface

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

type
  TPicDBGrid = class(TDBGrid)
  private
    { Private declarations }
    FPicture: TPicture;
    FPictureFieldName: string;
    FIndent: Integer;
    procedure SetPicture(Value: TPicture);
  protected
    { Protected declarations }
    procedure DrawColumnCell(const Rect: TRect; DataCol: Integer; Column: TColumn;
                             State: TGridDrawState); override;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    { Published declarations }
    property Indent: Integer read FIndent write FIndent;
    property Picture: TPicture read FPicture write SetPicture;
    property PictureFieldName: string read FPictureFieldName write FPictureFieldName;
  end;

procedure Register;

implementation

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

{ TPicDBGrid }

constructor TPicDBGrid.Create(AOwner: TComponent);
begin
  inherited;
  FPicture := TPicture.Create;
  FIndent := 0;
end;

destructor TPicDBGrid.Destroy;
begin
  FPicture.Free;
  inherited;
end;

procedure TPicDBGrid.DrawColumnCell(const Rect: TRect; DataCol: Integer;
  Column: TColumn; State: TGridDrawState);
begin
  inherited;
  if AnsiCompareText(Column.FieldName, PictureFieldName) = 0 then
    with Canvas do
    begin
      StretchDraw(Classes.Rect(Rect.Left + Indent,Rect.Top + Indent,
                               Rect.Right - Indent,Rect.Bottom - Indent),
                  FPicture.Bitmap);
    end;
end;

procedure TPicDBGrid.SetPicture(Value: TPicture);
begin
  FPicture.Assign(Value);
end;



end.
0
 
thirdAuthor Commented:
bes67,

  the picture property works fine with bitmaps but i can't get it to work with icons and jpegs. could you show me how?
0
 
thirdAuthor Commented:
ok i got it by just changing

FPicture.Bitmap  >> FPicture.Graphic

0
 
thirdAuthor Commented:
thanks! ;-)
0
 
bes67Commented:
Sorry, I was out :)

>> FPicture.Bitmap  >> FPicture.Graphic

Good decision

and you are welcome
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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