?
Solved

Custom DBGrid Component

Posted on 2002-05-28
10
Medium Priority
?
1,173 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 30

Author Comment

by:third
ID: 7040906
"with Canvas do"

im sorry that should be

"with DBGrid1.Canvas do"
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 7041145
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
ID: 7041241
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Expert Comment

by:swift99
ID: 7055421
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
ID: 7055562
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
 
LVL 1

Accepted Solution

by:
bes67 earned 400 total points
ID: 7055789
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
ID: 7058024
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
ID: 7058153
ok i got it by just changing

FPicture.Bitmap  >> FPicture.Graphic

0
 
LVL 30

Author Comment

by:third
ID: 7058158
thanks! ;-)
0
 
LVL 1

Expert Comment

by:bes67
ID: 7058270
Sorry, I was out :)

>> FPicture.Bitmap  >> FPicture.Graphic

Good decision

and you are welcome
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

800 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