?
Solved

How to insert a Button in a DBGrid cell?

Posted on 2005-05-04
3
Medium Priority
?
4,223 Views
Last Modified: 2010-04-05
Hello Experts,

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

Sergio
0
Comment
Question by:SergioLobo
1 Comment
 
LVL 26

Accepted Solution

by:
Eddie Shipman earned 2000 total points
ID: 13931013
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

839 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