Solved

150pts CustomDBGrid Component

Posted on 2002-06-04
4
148 Views
Last Modified: 2010-04-04
0
Comment
Question by:third
  • 2
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
bes67 earned 50 total points
ID: 7055531
Hi

Try this unit. This is JUST an example. PicDBGrid inherited from DBGrid and publish two properties Picture and PictureFieldName (to determine where to draw picture). Note that in the cell assotiated with this field just picture will be drawn, not the query data

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;
    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 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;
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(Rect, FPicture.Bitmap);
    end;
end;

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



end.
0
 
LVL 30

Author Comment

by:third
ID: 7055654

Great, that did it exactly what i want! could you post your answer on the link i've given above so i can give you all the points.

btw, i have additional request. could it be possible to add a property that will control the spaces between the rows.

i have tried the code below but i'm not so sure if that's the right way to do it. it destroys the appearance of the image (i.e. not proportionally sized).

procedure TPicDBGrid.DrawColumnCell(const Rect: TRect; DataCol: Integer;
 Column: TColumn; State: TGridDrawState);
var
 OutRect : TRect;
begin
 inherited;
 if AnsiCompareText(Column.FieldName, PictureFieldName) = 0 then
   with Canvas do
   begin
     OutRect := Rect;
     InflateRect(OutRect,-2,-2);
     StretchDraw(OutRect, FPicture.Bitmap);
   end;
end;

thanks again! ;-)
0
 
LVL 1

Expert Comment

by:bes67
ID: 7056287
I have posted on given link and ... take a look on Indent property

0
 
LVL 30

Author Comment

by:third
ID: 7058161
here's the bonus... thanks again! ;-)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

13 Experts available now in Live!

Get 1:1 Help Now