Grid com colunas tipo checkbox

Delphi

21/06/2005

Alguem sabe como colocar em DBgrid ou mesmo Grid uma coluna tipo um checkbox ou se existe algum componente que faça isso. Detalhe: para Delphi 5


Flacandido

Flacandido

Curtidas 0

Respostas

Daia

Daia

21/06/2005

eu sei que tem um tal de checkdbGrid, tenta achar na net.


GOSTEI 0
Massuda

Massuda

21/06/2005

Acho que seria interessante você dar uma olhada no artigo [url=http://delphi.about.com/od/usedbvcl/l/aa081903a.htm]Adding components to a DBGrid[/url] que explica como embutir diferentes componentes no dbgrid; em particular, [url=http://delphi.about.com/library/weekly/aa082003a.htm]uma das partes do artigo[/url] mostra como embutir um checkbox.


GOSTEI 0
Sandra

Sandra

21/06/2005

Tem também o código fonte do artigo que saiu na edição 44 da revista ClubeDelphi - Segredos do DBGrid (Guinther Pauli).
http://forum.clubedelphi.net/viewtopic.php?t=29891&highlight=guinther+pauli


GOSTEI 0
Jairroberto

Jairroberto

21/06/2005

Olá, flacandido!

Você pode simular um check box com imagens, pintando a coluna do tipo Boolean de acordo com o valor do campo vinculado a ela. Veja um exemplo de código:

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
var
  r: TRect;
  Centro: Integer;
  b: TBitmap;
begin
  inherited;
  if Column.Field.DataType = ftBoolean then
  begin
    with TDBGrid(Sender) do
    begin
      DefaultDrawColumnCell(Rect, DataCol, Column, State);
      if not Column.Field.IsNull then
      begin
        b := TBitmap.Create;
        try
          if Column.Field.AsBoolean then
            b.LoadFromResourceName(HInstance, ´CHECK_ON´)
          else
            b.LoadFromResourceName(HInstance, ´CHECK_OFF´);
          Centro := ((Rect.Right - Rect.Left + 1) div 2) + Rect.Left;
          r.Top := Rect.Top + 2;
          r.Bottom := r.Top + b.Height;
          r.Left := Centro - ((b.Width + 1) div 2);
          r.Right := r.Left + b.Width;
          Canvas.FillRect(Rect);
          Canvas.Draw(r.Left, r.Top, b);
        finally
          b.Free;
        end;
      end;
    end;
  end
  else
    TDBGrid(Sender).DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;


Você também pode usar o mouse para alterar o valor do campo:

procedure TForm1.DBGrid1CellClick(Column: TColumn);
begin
  if Column.Field.DataType = ftBoolean then
  begin
    if not (Column.Field.DataSet.State in dsEditModes) then
      Column.Field.DataSet.Edit;
    Column.Field.AsBoolean := not Column.Field.AsBoolean;
  end;
end;



Um abraço,
Jair


GOSTEI 0
POSTAR