Solved

Custom DBGrid Component

Posted on 2002-05-28
10
1,157 Views
Last Modified: 2007-12-19
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
Comment
Question by:third
10 Comments
 
LVL 30

Author Comment

by:third
Comment Utility
"with Canvas do"

im sorry that should be

"with DBGrid1.Canvas do"
0
 
LVL 14

Expert Comment

by:DragonSlayer
Comment Utility
I think you can just replace it with

with DBGrid1.Canvas do
  CopyRect(Rect, Image1.Canvas, Image1.Canvas.ClipRect);
0
 
LVL 30

Author Comment

by:third
Comment Utility
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
 
LVL 6

Expert Comment

by:swift99
Comment Utility
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
 
LVL 1

Expert Comment

by:alx512
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Accepted Solution

by:
bes67 earned 100 total points
Comment Utility
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
 
LVL 30

Author Comment

by:third
Comment Utility
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
 
LVL 30

Author Comment

by:third
Comment Utility
ok i got it by just changing

FPicture.Bitmap  >> FPicture.Graphic

0
 
LVL 30

Author Comment

by:third
Comment Utility
thanks! ;-)
0
 
LVL 1

Expert Comment

by:bes67
Comment Utility
Sorry, I was out :)

>> FPicture.Bitmap  >> FPicture.Graphic

Good decision

and you are welcome
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now