Buscar

Simulando checkbox em dbgrid

Código

{Tudo bom, eu não sei se existe algum exemplo assim no DTDelphi, eu vi essa dica e achei interessante, testei e aprovei, se existir alguma outra dica me informe, ai vai:
para colocar a figura do check, é só colocar um ImageList1, e adicionar nele as figuras do Check e do X, tipo confirmar e cancelar, feito isso crie um campo boleano para receber os valores para modificar a figura no grid: essa procedure pinta o Dbgrid com um checkbox}


procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  R: TRect;
begin
  {pinta checkbox}
  if Column.Field = ClientDataSet1ATIVO then
  begin
    DBGrid1.Canvas.FillRect(Rect);
    ImageList1.Draw(DBGrid1.Canvas, Rect.Left + 10, Rect.Top + 1, 0);
    if ClientDataSet1ATIVO.AsBoolean then
      ImageList1.Draw(DBGrid1.Canvas, Rect.Left + 10, Rect.Top + 1, 2)
    else
      ImageList1.Draw(DBGrid1.Canvas, Rect.Left + 10, Rect.Top + 1, 1);
  end;
end;

{essa daqui permite que o usuário clique com o mouse fazendo o checkbox mudar a figura.}

procedure TForm1.DBGrid1CellClick(Column: TColumn);
begin
  if Column.Field = ClientDataSet1ATIVO then
  begin
    ClientDataSet1.Edit;
    ClientDataSet1ATIVO.AsBoolean := not ClientDataSet1ATIVO.AsBoolean;
  end;
end;

{essa daqui permite o grid ficar em modo edição para modificar a figura}


procedure TForm1.DBGrid1ColEnter(Sender: TObject);
begin
  if DBGrid1.SelectedField = ClientDataSet1ATIVO then
    DBGrid1.Options := DBGrid1.Options - [dgEditing]
  else
    DBGrid1.Options := DBGrid1.Options + [dgEditing];
end;
 

Publicidade

Vote na dica




Quantidade de votos: 0 votos
Aceitação: 0%


Detalhes da dica

Categoria: Componentes
Adicionada dia: 28/10/05
Por: Jaime Fortunato De Lima
Visualizada: 31908 vezes

Planeta Delphi - Tudo sobre programação Delphi Planeta Delphi - www.planetadelphi.com.br - Todos os direitos reservados | Copyright 2001-2009