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

How to insert a Button in a DBGrid cell?

Hello Experts,

Does anyone know how I can insert a Button in a DBGrid's cell?

Sergio
0
SergioLobo
Asked:
SergioLobo
1 Solution
 
Eddie ShipmanAll-around developerCommented:
Try something like this:

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  r: TRect;
  style: DWORD;
begin
  r:= Rect;
  if (not (gdFixed in State)) and (DataCol = FButtonCol) then
  begin
    TDBGrid(Sender).Canvas.Brush.Color := clBtnFace;
    TDBGrid(Sender).Canvas.Font.Color  := clBtnText;
    style := DFCS_BUTTONPUSH or DFCS_ADJUSTRECT;
    if (FCellDown.X = DataCol) Then
      style := style or DFCS_PUSHED;
    DrawFrameControl( TDBGrid(Sender).Canvas.Handle, r, DFC_BUTTON, style );
  end;
  TDBGrid(Sender).DefaultDrawColumnCell(R, DataCol, Column, State);
end;

type TMyGrid = class(TCustomGrid);

procedure TForm1.DBGrid1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  cell: TGridCoord;
begin
  if (Button = mbLeft) and ((Shift - [ssLeft]) = []) then
  begin
    cell :=  TCustomGrid(Sender).MouseCoord(X, Y);
    if (cell.X >= 1) and (cell.Y >= 1) then
    begin
      FCellDown := cell;
      TMyGrid(Sender).InvalidateCell( cell.X, cell.Y );
    end;
    TMyGrid(Sender).InvalidateCell( FCellDown.X, FCellDown.Y );
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FButtonCol := 0;
  FCellDown.X := 999;
  FCellDown.Y := 999;
end;
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

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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