Faça sua buscar ...

sexta-feira, 23 de janeiro de 2015

Delphi - CheckBox no DBGrid.



Ola Pessoal,

Após um tempo de turbulência no trabalho, mais um post.
Vamos ao código pra fazer isso ;-)

1) Crie seu formulário, com o DBGrid conectado a tabela.
2) Selecione o seu DBGrid e acione o evento OnDrawColumnCell.
3) Coloque o seguinte código

procedure Tfrm_tarefas.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  Check: Integer;
  R: TRect;
begin
  if Column.FieldName = 'concluido' then //COLOQUE O NOME DA COLUNA, NO MEU CASO 'CONCLUIDO'
  begin
    DBGrid1.Canvas.FillRect(Rect);
    Check := 0;
    if tarefasconcluido.AsBoolean = true then
      Check := DFCS_CHECKED
    else
      Check := 0;
    R:=Rect;
    InflateRect(R,-2,-2); {Diminue o tamanho do CheckBox}
    DrawFrameControl(DBGrid1.Canvas.Handle,R,DFC_BUTTON, DFCS_BUTTONCHECK or Check);
  end;
end;
4) Selecione o seu DBGrid e acione o evento OnCellClick
5) Coloque o seguinte código:


procedure Tfrm_tarefas.DBGrid1CellClick(Column: TColumn);
begin
  tarefas.Edit;
  if tarefasconcluido.AsBoolean = true then
    tarefasconcluido.AsBoolean := false
  else
    tarefasconcluido.AsBoolean := true;
  tarefas.Post;
end;

Nenhum comentário:

Postar um comentário