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

adding other controls to TStringGrid cells

how can I replace standard string grid cells with check boxes comboboxes or other control,  how do I paint (images) withing the cells ?
0
amielDorel
Asked:
amielDorel
1 Solution
 
Ivanov_GCommented:
For drawing use OnCellDraw event:

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  // draw in [1,1] cell
  if (ACol = 1) and (ARow = 1) then
    begin
      Rect.Right := Rect.Right - 10;
      Rect.Bottom := Rect.Bottom - 10;
      StringGrid1.Canvas.Brush.Color := clRed;
      StringGrid1.Canvas.Pen.Color := clBlack;
      StringGrid1.Canvas.Rectangle(Rect);
    end;
end;

You can also choose some ready StringGrid component:
http://www.torry.net/pages.php?id=114
0
 
Eddie ShipmanAll-around developerCommented:
Here is Peter Below's contribution to the Borland newsgroups on how to add checkboxes to the column of a grid:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls, Grids;

type
  TForm1 = class(TForm)
    StatusBar1: TStatusBar;
    StringGrid1: TStringGrid;
    procedure FormCreate(Sender: TObject);
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure StringGrid1TopLeftChanged(Sender: TObject);
    procedure gridcheckboxMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
  chk: TCheckbox;
  i: integer;
begin
  with stringgrid1 do begin
    rowcount := 30;
    colcount := 10;
    for i:= fixedrows to rowcount-1 do begin
      cells[1,i] := ' ';
      chk:= TCheckbox.Create( stringgrid1 );
      with chk do begin
        name := format('CellChk%d_%d',[1,i]);
        caption := '';
        checked := false;
        visible := false;
        parent  := stringgrid1;
        width   := height;
        OnMouseDown := gridcheckboxMouseDown;
      end;
      Objects[1,i] := chk;
    end;
  end;
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  chk: TCheckbox;
  grid: TStringgrid;
begin
  grid := Sender As TStringgrid;
  if (aCol = 1) and (aRow >= grid.FixedRows)
  then begin
    chk:= TCheckbox(grid.Objects[ aCol, aRow ]);
    If Assigned(chk) Then Begin
      chk.SetBounds( (rect.left + rect.right - chk.width) div 2,
                     (rect.top + rect.bottom - chk.height) div 2,
                     chk.width, chk.height );
      if not chk.visible then
        chk.show;
    End;
  end;

end;

type
  tgridcracker = Class( tstringgrid );

procedure TForm1.StringGrid1TopLeftChanged(Sender: TObject);
begin
  tgridcracker(Sender).InvalidateCol(1);
end;

procedure TForm1.gridcheckboxMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  With Sender As TCheckbox Do
    Checked := not Checked;
end;

end.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

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